2012-01-02 4 views
4

私はGoogle PHP APIを使用しています。ドキュメントはかなり暗いです...ユーザーが自分のGoogle+情報に接続できるようにしたいのですが、これを使ってデータベースに登録することもできます。そのためには、Googleアカウントで使用する電子メールを取得する必要があります。私はどのように理解するように見えることはできません。ユーザーが自分のアプリに接続するときに許可を強制することで、Facebookで簡単にできる。誰もが考えている?これはユーザーのGoogle +プロフィールを取得するために使用しているコードであり、ユーザーが自分のメールアドレスをリストに載せていない場合を除き、うまくいきます。ユーザーの電子メールアドレスなしPHP用Google+ APIの使用 - ユーザーのメールを取得する必要があります

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に慣れて誰もが、私はそれを得ることができる方法を知っているGoogle+のでサインアップすることを可能にする目的に反し?

答えて

2

私に何か不足している場合は私に許してください。ただし、メールアドレスをお持ちでない場合は、どのGoogleアカウントをリンクさせるべきですか?通常は、最初にユーザーのプロフィールを見つけるために、Googleアカウントのメールアドレスを入力するようにユーザーに指示します。

OAuth2を使用すると、scopeパラメータを使用してアクセス許可を要求できます。 (Documentation.)あなたが望む範囲がhttps://www.googleapis.com/auth/userinfo.emailhttps://www.googleapis.com/auth/userinfo.profileであると想像します。

次に、アクセストークンを取得すると簡単にget the profile infoになります。 (私はあなたがアクセストークンのために返された認証コードを償還することができましたと仮定?)だけhttps://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken}にGETリクエストを行い、電子メールを含む、プロファイルデータの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のように自分のアプリケーションをOAuthしています。唯一の問題は、メールアドレスを取得して自分のデータベースに入力できるようにする方法を理解できないことです。完全なスクリプトのコードを更新しますが、すべての機密コードを持つ "$ client"変数を省略します。 – Throttlehead

+0

申し分なく、いいですね。 – benesch

+0

http://code.google.com/p/google-api-php-client/こちらをご覧ください。 – Throttlehead

10

のUserInfo APIは現在Googleがサポートされています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'; 

幸運です。今日のAPIを更新しました

+0

これにコメントできますか:http://stackoverflow.com/questions/11495303/error-fetching-oauth2-access-token-500-error –

7

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

これは、次のことを前提としています

  1. あなたが適切なスコープを持つ現在のユーザーを認証しました。
  2. $ clientをclient_idとclient_secretで設定しました。
+1

これは私のために働いた: $ google_plus = new Google_Service_Oauth2($ client); $ google_plus-> userinfo-> get() – Nico

関連する問題