Google Api via PHP : Get User Profile

我已经通过Google API(使用Google php api客户端)添加了网站登录,我不确定如何获得用户配置文件。


进行身份验证的代码为:


$client = new Google_Client();

$client->setClientId(ClientIDGoesHere);

$client->setClientSecret(ClientSecretGoesHere);

$client->setScopes(Google_Service_Gmail::GMAIL_READONLY);


$redirectUrl = 'http://AReference.ngrok.io/performGoogleLogin.php';

$client->setRedirectUri($redirectUrl);


$oauth = $client->getOAuth2Service();

我从这个网站尝试了以下内容,但它说找不到用户信息:


$userProfile = $oauth->userinfo->get();

我收到以下错误:注意:未定义的属性:Google\Auth\OAuth2::$userinfo在googleLogin中.php第23行


我追求given_name,family_name,电子邮件,性别,图片(很高兴有)


明月笑刀无情
浏览 110回答 1
1回答

慕盖茨4494581

OP和@user9156598,这是对我有用的解决方案。$oauth = new Google_Service_Oauth2($googleClient->GetClient());$userProfile = $oauth->userinfo->get();$output = "<p>Name: " .&nbsp; $userProfile['name'] . '</p>';$output = $output . "<p>Family Name: " .&nbsp; $userProfile['familyName'] . '</p>';$output = $output . "<p>Given Name: " .&nbsp; $userProfile['givenName'] . '</p>';$output = $output . "<p>Gender: " .&nbsp; $userProfile['gender'] . '</p>';$output = $output . "<p>Email Address: " .&nbsp; $userProfile['email'] . '</p>';$output = $output . "<p>Verified Email Address: " .&nbsp; $userProfile['verifiedEmail'] . '</p>';$output = $output . "<p>Picture: " .&nbsp; $userProfile['picture'] . '</p>';print($output);谷歌客户端构造函数:&nbsp; &nbsp; $this->m_client = new Google_Client();&nbsp; &nbsp; $this->m_client->setApplicationName("Local Social Meetups");&nbsp; &nbsp; $this->m_client->setClientId(GOOGLE_CLIENT_ID);&nbsp; &nbsp; $this->m_client->setClientSecret(GOOGLE_CLIENT_SECRET);&nbsp; &nbsp; $this->m_client->setIncludeGrantedScopes(true);&nbsp; &nbsp; $this->m_client->setAccessType('offline');&nbsp; &nbsp; $this->m_client->setApprovalPrompt("force");&nbsp; &nbsp; $this->m_client->setIncludeGrantedScopes(true);&nbsp; &nbsp; // Set the scope of information that the application has access to.&nbsp; In&nbsp; &nbsp; // this case it's just the users profile.&nbsp; &nbsp; $scopes = array(&nbsp; &nbsp; &nbsp; &nbsp; Google_Service_Oauth2::USERINFO_PROFILE,&nbsp; &nbsp; &nbsp; &nbsp; Google_Service_Oauth2::USERINFO_EMAIL&nbsp; &nbsp; );&nbsp; &nbsp; $this->m_client->setScopes($scopes);&nbsp; &nbsp; // The redirection Url that is used by Google after authentication.&nbsp; &nbsp; $this->m_client->setRedirectUri(GOOGLE_REDIRECT_URL);&nbsp; &nbsp; if (isset($_SESSION['googleAccessToken']))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->m_client->setAccessToken($_SESSION['googleAccessToken']);&nbsp; &nbsp; }&nbsp; &nbsp; $this->m_oauth = $this->m_client->getOAuth2Service();
打开App,查看更多内容
随时随地看视频慕课网APP