2015-07-04 61 views
1

你能告诉我,我怎样才能通过Google+ Javascript/PHP的API获取用户数据(姓名,电子邮件)?我按照这个tutorial去,它描述了如何通过Javascript获得authResult['code']通过Javascript和PHP登录Google+

这就是我确定。我通过Ajax将它发送到服务器,但我不知道如何通过PHP BUT获取用户数据,如姓名和电子邮件,而无需任何重定向。下面是代码 的JavaScript样本:

function googleStart() 
{ 
    console.log('googleStart'); 
    gapi.load('auth2', function() 
    { 
     auth2 = gapi.auth2.init({ 
      client_id: '811214467813-v3fmui5dfghte5m0kmohsf6dl11ori3tg.apps.googleusercontent.com', 
      // Scopes to request in addition to 'profile' and 'email' 
      fetch_basic_profile: false, 
      scope: 'profile email' 
     }); 
    }); 
} 

function googleSignIn(authResult) 
{ 
    console.log(authResult); 

    if (authResult['code']) { 

     console.log(authResult); 

     // Hide the sign-in button now that the user is authorized, for example: 
     $('#signinButton').attr('style', 'display: none'); 

     // Send the code to the server 
     $.ajax({ 
      type: 'POST', 
      url : "{link :Signgoogle:in}", 
      accepts : 'json', 
      // Spýtať sa na DJPW čo to tu je zakomentované 
      //contentType: 'application/octet-stream; charset=utf-8', 
      //processData: false, 
      data : { 
       'code' : authResult['code'], 
       'id_token' : authResult.getAuthResponse().id_token 
      }, 
... 

PHP:

public function actionIn() 
{ 
    $code = $_POST['code']; 

    $client = new \Google_Client(); 
    $client->setClientId(self::APP_ID); 
    $client->setClientSecret(self::APP_SECRET); 
    $client->setRedirectUri(self::REDIRECT_URI); 

    $client->authenticate($code); 
    $access_token = $client->getAccessToken(); 
    $client->setAccessToken($access_token); 

    $client->getAuth(); 


    $data = $access_token; 

    if($data) 
    { 
     $this->sendJson(array($data)); 
    } 
    else 
    { 
     $data = array('error' => '$client->verifyIdToken($id) skočil chybou.'); 
     $this->sendJson($data); 
    } 

} 
.... 

PHP检索一个access_token但没有用户数据。当然,这是一个Ajax调用,所以我不能像Google在每个页面上描述的那样执行任何重定向。你能帮我吗,我找不到任何解决方案。

非常感谢。

回答

2

一旦你有一个用户认证的客户端,你会想要向Google+ people.get API method发出请求。

$client = new Google_Client(); 
$client->setClientId(CLIENT_ID); 
$client->setClientSecret(CLIENT_SECRET); 
$client->setRedirectUri(REDIRECT_URI); 
$client->setAccessToken($access_token); 
$plus = new Google_Service_Plus($client); 
$me = $plus->people->get('me'); 
print "ID: {$me['id']}\n"; 
print "Display Name: {$me['displayName']}\n"; 
print "Image Url: {$me['image']['url']}\n"; 
print "Url: {$me['url']}\n"; 

对于他们email你将不得不遍历$me['emails']阵列和找到type=account值。

+0

首先谢谢。但是,$加上变量从哪里来?这是什么?我不是女巫。 –

+0

我发现这个代码:$ plus = new \ Google_PlusService($ client);但它会抛出异常:未找到类“Google_PlusService”。我已经通过Composer安装Google API:require:{“google/apiclient”:“1.0.*@beta”} –

+0

这是疯了!看来正确的形式是$ plus = new \ Google_Service_Plus($ client); –

2

,如果你不使用谷歌的客户端库,您可以使用谷歌API来获取客户的详细资料,你只要传递令牌这个API https://www.googleapis.com/oauth2/v3/tokeninfo?id_token= < your_token>

$response = file_get_contents ('https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' . $_REQUEST['token']); 

$response = json_decode ($response); 

echo "<pre>"; 
print_r ($response); 
echo "</pre>"; 

这将验证用户,它会返回对象

stdClass Object 
(
    [iss] => accounts.google.com 
    [at_hash] => hE0R********nxg 
    [aud] => 79*********************p.apps.googleusercontent.com 
    [sub] => 1028*****2 
    [email_verified] => true 
    [azp] => 7945823****p.apps.googleusercontent.com 
    [email] => bik***@gmail.com 
    [iat] => 14***7 
    [exp] => 1***07 
    [name] => Bik****M 
    [picture] => https://lh6.googleusercontent.com/-W8cA***/photo.jpg 
    [given_name] => Bike*** 
    [family_name] => M 
    [locale] => en 
    [alg] => RS256 
    [kid] => 70f6c4267*******cb 
) 

源泉这里一个例子:http://wiki.workassis.com/implementing-google-sign-in-for-websites