2012-01-02 104 views
4

我正在使用Google PHP API。文檔相當乏味......我想讓用戶連接他們的Google+信息,並使用它來爲我的數據庫中的用戶簽名。爲了做到這一點,我需要獲得他們用於他們的谷歌帳戶的電子郵件。我似乎無法弄清楚如何。當用戶連接到我的應用程序時,通過強制許可,Facebook很容易。任何人有任何想法?這是我用來抓取用戶google +個人資料的代碼,並且工作正常,除非用戶可能沒有在那裏列出他們的電子郵件。使用Google+ API for PHP - 需要獲取用戶電子郵件

include_once("$DOCUMENT_ROOT/../qwiku_src/php/google/initialize.php"); 

$plus = new apiPlusService($client); 
if (isset($_GET['code'])) { 
    $client->authenticate(); 
    $_SESSION['token'] = $client->getAccessToken(); 
    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); 
} 

if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']); 
} 

if ($client->getAccessToken()) { 
    $me = $plus->people->get('me'); 
    print "Your Profile: <pre>" . print_r($me, true) . "</pre>"; 
    // The access token may have been updated lazily. 
    $_SESSION['token'] = $client->getAccessToken(); 
} else { 
    $authUrl = $client->createAuthUrl(); 
    print "<a class='login' href='$authUrl'>Connect Me!</a>"; 
} 

如果沒有用戶的電子郵件地址,它像是失敗讓用戶與Google+的人比較熟悉的谷歌API註冊知道我怎樣才能得到它的目的是什麼?

回答

2

如果我錯過了某些東西,請原諒我,但如果您沒有自己的電子郵件地址,您如何知道Google帳戶將其鏈接到哪個帳戶?通常,您會提示用戶輸入他們的Google帳戶的電子郵件,以便首先找到他們的個人資料。

使用OAuth2,您可以通過參數scope請求權限。 (Documentation.)我想象你想要的示波器是https://www.googleapis.com/auth/userinfo.emailhttps://www.googleapis.com/auth/userinfo.profile

然後,一旦獲得訪問令牌,對於get the profile info來說就簡單了。 (我假設你已經能夠贖回返回的授權代碼的訪問令牌?)只是做一個GET請求https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken},返回文件數據的JSON陣列,包括電子郵件:

{ 
"id": "00000000000000", 
"email": "[email protected]", 
"verified_email": true, 
"name": "Fred Example", 
"given_name": "Fred", 
"family_name": "Example", 
"picture": "https://lh5.googleusercontent.com/-2Sv-4bBMLLA/AAAAAAAAAAI/AAAAAAAAABo/bEG4kI2mG0I/photo.jpg", 
"gender": "male", 
"locale": "en-US" 
} 

我們有了成爲PHP庫中的一個方法來提出請求,但我找不到它。 無擔保,但試試這個:

$url = "https://www.googleapis.com/oauth2/v1/userinfo"; 
$request = apiClient::$io->makeRequest($client->sign(new apiHttpRequest($url, 'GET'))); 

if ((int)$request->getResponseHttpCode() == 200) { 
     $response = $request->getResponseBody(); 
     $decodedResponse = json_decode($response, true); 
     //process user info 
     } else { 
     $response = $request->getResponseBody(); 
     $decodedResponse = json_decode($response, true); 
     if ($decodedResponse != $response && $decodedResponse != null && $decodedResponse['error']) { 
      $response = $decodedResponse['error']; 
     } 
     } 
} 

不管怎樣,在你發佈的代碼,只是通過所需的範圍createAuthUrl()

else { 
    $authUrl = $client->createAuthUrl("https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"); 
    print "<a class='login' href='$authUrl'>Connect Me!</a>"; 
} 
+1

我使用的是Google+服務,他們像我在Facebook一樣使用我的應用程序。唯一的問題是,我不知道如何確保獲得一個電子郵件地址,所以我可以將它們輸入到我的數據庫中。我將更新完整腳本的代碼,但忽略具有所有敏感代碼的「$ client」變量。 – Throttlehead 2012-01-03 03:35:20

+0

好吧,聽起來不錯。 – benesch 2012-01-03 03:37:07

+0

http://code.google.com/p/google-api-php-client/更多信息在這裏。 – Throttlehead 2012-01-03 03:37:26

10

用戶信息API現在由谷歌支持API PHP客戶端。

這裏是一個小示例應用程序: http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/userinfo/index.php

樣品的最重要的一點是在這裏:

$client = new apiClient(); 
    $client->setApplicationName(APP_NAME); 
    $client->setClientId(CLIENT_ID); 
    $client->setClientSecret(CLIENT_SECRET); 
    $client->setRedirectUri(REDIRECT_URI); 
    $client->setDeveloperKey(DEVELOPER_KEY); 
    $client->setScopes(array('https://www.googleapis.com/auth/userinfo.email', 
     'https://www.googleapis.com/auth/plus.me'));  // Important! 

    $oauth2 = new apiOauth2Service($client); 

    // Authenticate the user, $_GET['code'] is used internally: 
    $client->authenticate(); 

    // Will get id (number), email (string) and verified_email (boolean): 
    $user = $oauth2->userinfo->get(); 
    $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); 
    print $email; 

我假設這個片段被稱爲用GET請求包括授權碼。請記住包含或需要apiOauth2Service.php才能使其工作。在我的情況是這樣的:

require_once 'google-api-php-client/apiClient.php'; 
    require_once 'google-api-php-client/contrib/apiOauth2Service.php'; 

祝你好運。

+0

你能評論這個:http://stackoverflow.com/questions/11495303/error-fetching-oauth2-access-token-500-error – 2012-07-16 08:48:42

7

更新與今天的API:

$googlePlus = new Google_Service_Plus($client); 
$userProfile = $googlePlus->people->get('me'); 
$emails = $userProfile->getEmails(); 

這假定:

  1. 您已經驗證用適當的範圍當前用戶。
  2. 你已經用你的client_id和client_secret設置了$ client。
+1

這工作對我來說: $ google_plus =新Google_Service_Oauth2($客戶端); $ google_plus-> userinfo-> get() – Nico 2015-03-24 15:15:55

相關問題