我已经通过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();我在this website上尝试了以下命令,但提示找不到用户信息:
$userProfile = $oauth->userinfo->get();注意:未定义的属性: Google\Auth\OAuth2::$userinfo in googleLogin.php in googleLogin.php第23行
我正在寻找given_name,family_name,电子邮件,性别,图片(很高兴有)
发布于 2020-09-20 01:31:19
OP和@user9156598,这就是对我有效的解决方案。
$oauth = new Google_Service_Oauth2($googleClient->GetClient());
$userProfile = $oauth->userinfo->get();
$output = "<p>Name: " . $userProfile['name'] . '</p>';
$output = $output . "<p>Family Name: " . $userProfile['familyName'] . '</p>';
$output = $output . "<p>Given Name: " . $userProfile['givenName'] . '</p>';
$output = $output . "<p>Gender: " . $userProfile['gender'] . '</p>';
$output = $output . "<p>Email Address: " . $userProfile['email'] . '</p>';
$output = $output . "<p>Verified Email Address: " . $userProfile['verifiedEmail'] . '</p>';
$output = $output . "<p>Picture: " . $userProfile['picture'] . '</p>';
print($output);Google客户端构造函数:
$this->m_client = new Google_Client();
$this->m_client->setApplicationName("Local Social Meetups");
$this->m_client->setClientId(GOOGLE_CLIENT_ID);
$this->m_client->setClientSecret(GOOGLE_CLIENT_SECRET);
$this->m_client->setIncludeGrantedScopes(true);
$this->m_client->setAccessType('offline');
$this->m_client->setApprovalPrompt("force");
$this->m_client->setIncludeGrantedScopes(true);
// Set the scope of information that the application has access to. In
// this case it's just the users profile.
$scopes = array(
Google_Service_Oauth2::USERINFO_PROFILE,
Google_Service_Oauth2::USERINFO_EMAIL
);
$this->m_client->setScopes($scopes);
// The redirection Url that is used by Google after authentication.
$this->m_client->setRedirectUri(GOOGLE_REDIRECT_URL);
if (isset($_SESSION['googleAccessToken']))
{
$this->m_client->setAccessToken($_SESSION['googleAccessToken']);
}
$this->m_oauth = $this->m_client->getOAuth2Service();https://stackoverflow.com/questions/59935073
复制相似问题