Get Userinfo from Google OAuth 2.0 PHP API(从 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 = new Google_Oauth2Service($client);如果(isset($_REQUEST ['注销'])){未设置($_SESSION['access_token']);}如果(isset($_GET['代码'])){$client->authenticate($_GET['code']);$_SESSION['access_token'] = $client->getAccessToken();header('位置:http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);}如果(isset($_SESSION['access_token'])){$client->setAccessToken($_SESSION['access_token']);}if ($client->getAccessToken()){$userinfo = $plus->userinfo;print_r($userinfo->get());} 别的{$authUrl = $client->createAuthUrl();}?><!doctype html><头><meta charset="utf-8"><link rel='stylesheet' href='style.css'/>头部><身体><header><h1>Google+ 示例应用</h1></header><div class="box"><?php if(isset($personMarkup)): ?><div class="me"><?php print $personMarkup ?></div><?php endif ?><?php如果(isset($authUrl)){print "<a class='login' href='$authUrl'>连接我!</a>";} 别的 {打印 "<a class='logout' href='?logout'>Logout</a>";}?>
谢谢...
**编辑***缺少范围--已添加
$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 获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 Google OAuth 2.0 PHP API 获取用户信息
基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01