首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google Api通过PHP :获取用户配置文件

Google Api通过PHP :获取用户配置文件
EN

Stack Overflow用户
提问于 2020-01-28 00:22:37
回答 1查看 299关注 0票数 0

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

获取身份验证的代码为:

代码语言:javascript
运行
复制
$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上尝试了以下命令,但提示找不到用户信息:

代码语言:javascript
运行
复制
$userProfile = $oauth->userinfo->get();

注意:未定义的属性: Google\Auth\OAuth2::$userinfo in googleLogin.php in googleLogin.php第23行

我正在寻找given_name,family_name,电子邮件,性别,图片(很高兴有)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-20 01:31:19

OP和@user9156598,这就是对我有效的解决方案。

代码语言:javascript
运行
复制
$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客户端构造函数:

代码语言:javascript
运行
复制
    $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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59935073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档