为了账号安全,请及时绑定邮箱和手机立即绑定

Google Api via PHP : Get User Profile

Google Api via PHP : Get User Profile

PHP
明月笑刀无情 2022-08-19 15:52:40
我已经通过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,电子邮件,性别,图片(很高兴有)
查看完整描述

1 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

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);

谷歌客户端构造函数:


    $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();


查看完整回答
反对 回复 2022-08-19
  • 1 回答
  • 0 关注
  • 109 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号