2012-08-10 56 views
7

我使用“google-api-php-client”-Library(http://code.google.com/p/google-api-php-client/)中提供的示例来实现用户登录和授权在我的网站上使用谷歌服务。 我没有做任何改变的例子,除了添加我的客户端ID等。如何存储谷歌API(OAuth 2)的权限?

授权本身工作正常:用户可以登录,我可以获取提供的信息。 但是,当离开页面时,整个授权过程再次被调用;用户不会被记住,并且需要再次授予权限,这是一种令人讨厌的行为,而且对于谷歌登录而言并不典型,因为我知道它们。

例如:在stackoverflow上,我使用我的Google帐户登录。 每当我重新访问这个网站,我自动登录,或(如果注销)只需再次登录 - 我不必再次确认一般权利。 然而,在我的网站上使用示例强制用户在网站再次访问时允许访问。

使用示例时我犯过错吗? 我必须做些什么,以避免一遍又一遍的许可请求?

在此先感谢您的帮助!

回答

0

的谷歌驱动器SDK文档包括可以作为参考使用完整的PHP示例应用程序开始:

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

基本上,一旦用户登录,您检索访问令牌和刷新令牌,您将这些凭据存储在数据库中并重复使用它们,而不是每次都要求用户进行身份验证。一旦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); 
      $client->authenticate(); 
      $_SESSION['token'] = $client->getAccessToken(); 
      const ACCESS_TOKEN=$_SESSION['token']; 
       //code here to save in database 
    ?> 

<?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

谢谢,克劳迪奥!该链接看起来很有趣。 看来,我需要某种“虚拟代币” - 指南。 :-) 同时我发现, '$ client-> setAccessType(“online”); $ client-> setApprovalPrompt(“auto”);'解决问题的一部分。 我试图将令牌客户端存储为cookie,但没有成功... – Elvis 2012-08-11 06:38:01

+0

在此服务器上找不到请求的URL。 – yesitsme 2016-09-04 05:04:14

1

使用此代码的第一次检索access_code并将其保存到数据库中。 根据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 
?> 
+0

如果我想存储刷新令牌,因为我想在用户未登录时在后台调用API,那么这是如何改变的。 – Mikeys4u 2014-08-05 13:22:56

1

这对我来说工作正常