Javascript 如何通过谷歌登录获取用户名和姓氏?我已经收到电子邮件了

Javascript 如何通过谷歌登录获取用户名和姓氏?我已经收到电子邮件了,javascript,php,google-plus,Javascript,Php,Google Plus,请参阅本文底部的答案,谢谢这个家伙: 好吧,我已经盯着谷歌文档看了很久了(5个小时!) 我想要的只是基本的电子邮件,名字和姓氏,到目前为止我所能得到的只是电子邮件 谷歌文档并没有让我走得很远,似乎有太多的变化 这是我从google文档、stackoverflow文档和其他github存储库混合而成的php代码。$attributes中的最终结果包含一个“payload”数组,其中包含电子邮件和一些其他内容 <?php //load the google client set_include

请参阅本文底部的答案,谢谢这个家伙:

好吧,我已经盯着谷歌文档看了很久了(5个小时!)

我想要的只是基本的电子邮件,名字和姓氏,到目前为止我所能得到的只是电子邮件

谷歌文档并没有让我走得很远,似乎有太多的变化

这是我从google文档、stackoverflow文档和其他github存储库混合而成的php代码。$attributes中的最终结果包含一个“payload”数组,其中包含电子邮件和一些其他内容

<?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();
我是否可以通过另一个呼叫$client来获取用户的名字和姓氏

我很确定我是要把它插进去的:不知怎么的,但我看不到一条清晰的道路

回答:

<?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();

不确定这是否会对您有所帮助,但我使用了一个名为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中使用的唯一库。Google+API的
方法就是你想要的方法,这是正确的

以下是一个可以帮助您的示例:

除了像您已经在做的那样加载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'];

谢谢,我会记住这一点,但我确实希望最好使用谷歌api。我相信这属于。谢谢亚伯拉罕,我必须检查一下。我看不到src中的contrib目录。这是额外下载吗?这里有一个require_once“Google/Service/Plus.php”;如何使用谷歌文档就像在一英里长的沙纸上拖动我的脸!Scaryman您是对的,但有了新的API,sytax略有不同:投入postAh后,没有意识到有一个全新版本的php客户端库,很高兴您能够使用它:)
...

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'];