从Google OAuth 2.0 PHP API获取用户信息 [英] Get Userinfo from Google OAuth 2.0 PHP API

查看:166
本文介绍了从Google OAuth 2.0 PHP API获取用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用Google Oauth API获取用户信息。
它适用于Google Plus API,但我试图在用户没有Google Plus帐户的情况下创建备份。
身份验证过程是正确的,我甚至可以获取$ userinfo对象,但我如何访问属性。我尝试了$ userinfo-> get(),但它只返回用户的id。



我做错了什么?这里是我使用的代码...



  require_once'../../src/Google_Client。 PHP的; 
require_once'../../src/contrib/Google_Oauth2Service.php';

session_start();

$ client = new Google_Client();
$ client-> setApplicationName(Google+ PHP Starter Application);
//访问https://code.google.com/apis/console以生成
// oauth2_client_id,oauth2_client_secret,并注册您的oauth2_redirect_uri。
$ client-> setClientId('*********************');
$ client-> setClientSecret('**************');
$ client-> setRedirectUri('***************');
$ client-> setDeveloperKey('**************');
$ plus =新Google_Oauth2Service($ client);

if(isset($ _ REQUEST ['logout'])){
unset($ _ SESSION ['access_token']);


if(isset($ _ GET ['code'])){
$ client-> authenticate($ _ GET ['code']);
$ _SESSION ['access_token'] = $ client-> getAccessToken();
header('Location:http://'。$ _SERVER ['HTTP_HOST']。$ _SERVER ['PHP_SELF']);


if(isset($ _ SESSION ['access_token'])){
$ client-> setAccessToken($ _ SESSION ['access_token']);
}

if($ client-> getAccessToken())
{
$ userinfo = $ plus-> userinfo;
print_r($ userinfo-> get());

} else
{
$ authUrl = $ client-> createAuthUrl();
}
?>
<!doctype html>
< html>
< head>
< meta charset =utf-8>
< link rel ='stylesheet'href ='style.css'/>
< / head>
< body>
< header>< h1> Google+示例应用< / h1>< / header>
< div class =box>

<?php if(isset($ personMarkup)):?>
< div class =me><?php print $ personMarkup?>< / div>
<?php endif?>

<?php
if(isset($ authUrl)){
print< a class ='login'href ='$ authUrl'> Connect Me! < / A>中;
} else {
print< a class ='logout'href ='?logout'>注销< / a>;
}
?>
< / div>
< / body>
< / html>

谢谢...

**编辑* * *
缺少范围 - 已添加<​​b>

  $ client-> setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo的.profile')); 

现在可以使用...

解决方案

缺少范围

  $ client-> setScopes(array('https:// www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile')); 

现在就像魅力一样!


I am trying to use Google Oauth API to get userinfo. It works perfectly for Google Plus API but I am trying to create a backup in case the user doesn't have google plus account. The authentication process is correct and I even get the $userinfo object but how exactly do I access the properties. I tried $userinfo->get() but it only return the id of the user.

Am I doing something wrong? Here is the code that I am using...

require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_Oauth2Service.php';

session_start();

$client = new Google_Client();
$client->setApplicationName("Google+ PHP Starter Application");
// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
 $client->setClientId('*********************');
 $client->setClientSecret('**************');
 $client->setRedirectUri('***************');
 $client->setDeveloperKey('**************');
$plus = new Google_Oauth2Service($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['access_token'])) {
  $client->setAccessToken($_SESSION['access_token']);
}

if ($client->getAccessToken()) 
{
    $userinfo = $plus->userinfo;
    print_r($userinfo->get());

} else 
{
    $authUrl = $client->createAuthUrl();
}
?>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <link rel='stylesheet' href='style.css' />
</head>
<body>
<header><h1>Google+ Sample App</h1></header>
<div class="box">

<?php if(isset($personMarkup)): ?>
<div class="me"><?php print $personMarkup ?></div>
<?php endif ?>

<?php
  if(isset($authUrl)) {
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
  } else {
   print "<a class='logout' href='?logout'>Logout</a>";
  }
?>
</div>
</body>
</html>

Thanks...

**EDIT*** Was missing Scopes--Added

 $client->setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'));

works now...

解决方案

Was missing scopes

$client->setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'));

Works like a charm now!

这篇关于从Google OAuth 2.0 PHP API获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆