【问题标题】:Get user info using Google API OAuth in PHP在 PHP 中使用 Google API OAuth 获取用户信息
【发布时间】:2018-12-21 09:00:35
【问题描述】:

我已经在我的 php 网站中使用 Google_Client 登录了谷歌。

登录成功,但我正在尝试获取已登录但未获取的用户详细信息。

下面是我的代码。

$gClient = new Google_Client();
$gClient->setApplicationName('Login to -------');
$gClient->setClientId(GOOGLE_CLIENT_ID);
$gClient->setClientSecret(GOOGLE_CLIENT_SECRET);
$gClient->addScope(Google_Service_Oauth2::USERINFO_PROFILE);
$gClient->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
$gClient->addScope(Google_Service_Oauth2::PLUS_LOGIN);
$gClient->setAccessType('offline');        // offline access
$gClient->setIncludeGrantedScopes(true);   // incremental auth
$gClient->setRedirectUri(WEB_PANEL_PATH);
$google_code = $gh->read('code');
$objOAuthService = new Google_Service_Oauth2($gClient);
if (!empty($google_code)) {
  $gClient->authenticate($google_code);
  $_SESSION['access_token'] = $gClient->getAccessToken();
  $outputjson['active'] = $_SESSION['access_token'];
}
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $gClient->setAccessToken($_SESSION['access_token']);
}
if($gClient->getAccessToken()) {
  $userData = $objOAuthService->userinfo->get();
  if(!empty($userData)) {
  }
  $outputjson['user_data'] = $userData;
  $outputjson['access_token'] = $gClient->getAccessToken();
}else{
  $outputjson['login_url'] = $gClient->createAuthUrl();
}
$outputjson['session'] = $_SESSION;
$outputjson['success'] = '1';

$userData 始终为空。

它正在向 oauth2/v2/userinfo 发送请求,但没有获取任何数据。

[methods:Google_Service_Resource:private] => Array
(
    [get] => Array
    (
        [path] => oauth2/v2/userinfo
        [httpMethod] => GET
        [parameters] => Array
        (
        )
    )
)

【问题讨论】:

    标签: php google-api google-api-php-client google-oauth


    【解决方案1】:

    尝试执行以下操作

    var_dump($oauth->userinfo->get());
    

    应该给你这样的东西

    Array
    (
        [id] => 1045636599999999999
        [name] => Linda Lawton
        [given_name] => Linda
        [family_name] => Lawton
        [locale] => dk
    )
    

    这应该允许您执行$userData->parameters[0]$userData->parameters[id]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-10
      • 2013-01-27
      • 2020-02-04
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      相关资源
      最近更新 更多