2012-08-10 12 views
7

「google-api-php-client」-Library(http://code.google.com/p/google-api-php-client/)で提供されている例を使用して、ユーザーのログインと承認を実装しています私のウェブサイトでGoogleサービスを利用しています。 私はClient-IDなどを追加する以外は、例を変更しませんでした。google api(OAuth 2)の権限を保存するにはどうすればよいですか?

ユーザー自身がログインでき、提供された情報を取得できます。 しかし、ページを離れるときに、承認手続き全体が再度呼び出されます。ユーザーは記憶されておらず、アクセス許可を再度与える必要があります。これは、わたしが知っているように、グーグルログインの典型的ではない迷惑なものです。

例:stackoverflowで、私は自分のGoogleアカウントでログインしています。 私はこのサイトに再度アクセスするたびに、私は自動的にログインしています、または(ログアウトした場合)ただ再度ログインする必要があります - 私は再度一般的な権利を確認する必要はありません。 しかし、私のサイトの例を使用すると、サイトに再びアクセスしたときにいつでもアクセスできるようになります。

例を使用する際に間違いをしましたか? 許可要求を何度も繰り返さないようにするには、何が必要ですか?

ご協力いただきありがとうございます。

答えて

0

GoogleドライブSDKのドキュメントは、あなたが始めるための基準として使用することができ、完全なPHPサンプルアプリケーションが含まれます。ユーザーがログインしてあなたがアクセストークンとリフレッシュを取得されると、基本的に

https://developers.google.com/drive/examples/php

をこれらの資格情報をデータベースに格納し、毎回ユーザーに認証するかわりにそれらを再利用します。 access_codeを取得し、それをデータベースに保存するのは初めてのため

+0

ありがとう、クラウディオ!そのリンクは面白そうです。 私はある種の「ダミーのためのトークン」を必要としているようです。 :-) 私は、そのことを知りました '$ client-> setAccessType(" online "); $ client-> setApprovalPrompt( "auto");は問題の1つの部分を解決します。 クッキーとしてトークンのクライアント側を保存しようとしましたが、成功しませんでした... – Elvis

+0

要求されたURLはこのサーバー上に見つかりませんでした。 – yesitsme

1

使用このコードを:

<?php 
    require 'google-api-php-client/src/Google_Client.php'; 
    require 'google-api-php-client/src/contrib/Google_DriveService.php'; 
    require 'google-api-php-client/src/contrib/Google_Oauth2Service.php'; 
    session_start(); 

    $client = new Google_Client(); 
    $client->setClientId(CLIENT_ID); 
    $client->setClientSecret(CLIENT_SECRET); 
    $client->setRedirectUri(REDIRECT_URI); 
    $client->setScopes(array(
     'https://www.googleapis.com/auth/drive', 
     'https://www.googleapis.com/auth/userinfo.email', 
     'https://www.googleapis.com/auth/userinfo.profile')); 

    $client->setUseObjects(true); 
    $service = new Google_DriveService($client); 
      $client->authenticate(); 
      $_SESSION['token'] = $client->getAccessToken(); 
      const ACCESS_TOKEN=$_SESSION['token']; 
       //code here to save in database 
    ?> 

access_tokenはをにデータベース変更コードで保存されると:

<?php 
     require 'google-api-php-client/src/Google_Client.php'; 
     require 'google-api-php-client/src/contrib/Google_DriveService.php'; 
     require 'google-api-php-client/src/contrib/Google_Oauth2Service.php'; 
    session_start(); 

     $client = new Google_Client(); 
     $client->setClientId(CLIENT_ID); 
     $client->setClientSecret(CLIENT_SECRET); 
     $client->setRedirectUri(REDIRECT_URI); 
     $client->setScopes(array(
      'https://www.googleapis.com/auth/drive', 
      'https://www.googleapis.com/auth/userinfo.email', 
      'https://www.googleapis.com/auth/userinfo.profile')); 

     $client->setUseObjects(true); 
     $service = new Google_DriveService($client); 

    //ACCESS_TOKEN is already saved in database, is being saved on first time login. 

     $_SESSION['access_token'] = ACCESS_TOKEN; 

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

     if ($client->getAccessToken()) 
     { 
      $userinfo = $service->about->get(); 
      echo '<script>console.log('.json_encode($userinfo).');</script>'; 

      $userinfoService = new Google_OAuth2Service($client); 
      $user = $userinfoService->userinfo->get(); 
      echo '<script>console.log('.json_encode($user).');</script>'; 
     } 
    ?> 
+0

代わりにリフレッシュトークンを保存したい場合は、ユーザーがログインしていないときにAPIをバックグラウンドで呼び出したいので、それがどう変わるのですか? – Mikeys4u

1

私のために正常に動作します。 kaushalの答えに基づいて:

<?php 
require_once 'globals.php'; 
require_once 'google-api-php-client/src/Google_Client.php'; 
require_once 'google-api-php-client/src/contrib/Google_DriveService.php'; 

$client = new Google_Client(); 

// Get your credentials from the APIs Console 
$client->setClientId('YOUR_ID'); 
$client->setClientSecret('YOUR_SECRET'); 
$client->setRedirectUri('REDIRECT_URI'); 
$client->setScopes(array('https://www.googleapis.com/auth/drive')); 


$service = new Google_DriveService($client); 
$client->setUseObjects(true); 

//if no token in the session 
if ($_SESSION['google_token'] == '') { 
    //get stored token from DB 
    $sToken = $oDb->getOne("SELECT `google_token` FROM `users` WHERE `u_id` = " . (int)$_SESSION['user_id']); 
    //if no stored token in DB 
    if ($sToken == '') { 
     //autentificate user 
     $client->authenticate(); 
     //get new token 
     $token = $client->getAccessToken(); 
     //set token in session 
     $_SESSION['google_token'] = $token; 
     // set token in DB 
     $oDb->Query("UPDATE `users` SET `google_token`='$token' WHERE `u_id` = " . (int)$_SESSION['user_id']); 
    } else { 
     $_SESSION['google_token'] = $sToken; 
    } 
} 
$client->setAccessToken($_SESSION['google_token']); 

//do what you wanna do with clients drive here 
?> 
関連する問題