2015-07-04 69 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