看到本帖子的底部,感谢此人:How to get the users name and surname with google sign in? I have the email already
好的,我一直在盯着谷歌文档看似永恒(5小时!)!
我想要的只是基本的电子邮件,姓氏和姓氏,到目前为止,我能够得到的只是电子邮件。
谷歌文档确实没有让我走得太远,似乎有很多变化。
这是我从谷歌文档,stackoverflow文档和其他github存储库混合拼凑的PHP代码。 $ attributes中的最终结果包含带有电子邮件和其他一些
的“有效负载”数组<?php
//load the google client
set_include_path( WEBROOT_PRIVATE . 'authenticate/google-api-php-client/src/' . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
//set our unique params
$client_id = 'xxx';
$client_secret = 'xxx';
$app_name = 'xxx';
//start the google client
$client = new Google_Client();
$client->setApplicationName($app_name);
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri('postmessage');
// Exchange the OAuth 2.0 authorization code for user credentials.
$client->authenticate($_GET['code_from_client']);
$token = json_decode($client->getAccessToken());
$attributes = $client->verifyIdToken($token->id_token, CLIENT_ID)->getAttributes();
$gplus_id = $attributes["payload"]["sub"];
print_r($attributes);
die();
导致
Array
(
[envelope] => Array
(
[alg] => xxx
[kid] => xxx
)
[payload] => Array
(
[iss] => accounts.google.com
[sub] => xxx
[at_hash] => xxx
[azp] => xxx-xxx.apps.googleusercontent.com
[email_verified] => true
[aud] => xxx-xxx.apps.googleusercontent.com
[email] => xxx@gmail.com
[iat] => xxx
[exp] => xxx
)
)
是否有另一个电话我可以传递给$ client以获取用户的姓名和姓氏?
我很确定我打算将它插入:https://developers.google.com/+/api/latest/people/get不知怎的但是我看不清楚
解答:
<?php
//load the google client
set_include_path( WEBROOT_PRIVATE . 'authenticate/google-api-php-client/src/' . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Plus.php'; /* THIS IS REQUIRED */
//set our unique params
$client_id = 'XXX-XXX.apps.googleusercontent.com';
$client_secret = 'XXX';
$app_name = 'XXX';
//start the google client
$client = new Google_Client();
$client->setApplicationName($app_name);
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri('postmessage');
// Exchange the OAuth 2.0 authorization code for user credentials.
$client->authenticate($_GET['google_login_code']);
$token = json_decode($client->getAccessToken());
$attributes = $client->verifyIdToken($token->id_token, CLIENT_ID)->getAttributes();
$gplus_id = $attributes["payload"]["sub"];
// After $client is authenticated
$plus = new Google_Service_Plus($client); /* THEN PLACE THE $client INTO THE PLUS SERVICE */
$me = $plus->people->get('me');
$firstname = $me['name']['givenName'];
$lastname = $me['name']['familyName'];
print_r2($me);
print_r2($attributes);
die();
答案 0 :(得分:1)
您是正确的,因为Google+ API的people.get
方法就是您想要的方法。
以下是一个可以帮助您的示例:https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/plus/index.php
除了加载PHP客户端库的客户端之外,您还需要初始化Plus客户端以便能够调用people.get
方法。
...
require_once '../../src/contrib/Google_PlusService.php';
...
$client = new Google_Client();
...
$plus = new Google_PlusService($client);
...
// After $client is authenticated
$me = $plus->people->get('me');
$firstname = $me['name']['givenName'];
$lastname = $me['name']['familyName'];
答案 1 :(得分:0)
不确定这是否会对你有所帮助,但是我使用了一个名为LightOpenID的类:
$openid = new LightOpenID($doc_root);
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array(
'namePerson/first',
'namePerson/last',
'contact/email',
);
然后我可以使用:
$data = $openid->getAttributes();
$email = $data['contact/email'];
$first = $data['namePerson/first'];
这是我能提供的唯一帮助,因为这是我使用Google OpenID的唯一库