7

我正在使用“google-api-php-client”-Library (http://code.google.com/p/google-api-php-client/) 中提供的示例来实现我的用户登录和授权带有谷歌服务的网站。除了添加我的 Client-ID 等之外,我没有对示例进行任何更改。

授权本身工作正常:用户可以登录,我可以获取提供的信息。然而,当离开页面时,整个授权过程又被调用;用户不会被记住,需要再次授予权限,这对于我所知道的谷歌登录来说有点烦人且不典型。

例如:在 stackoverflow 上,我使用我的谷歌帐户登录。每当我重新访问此站点时,我都会自动登录,或者(如果已注销)只需再次登录 - 我不必再次确认一般权限。但是,使用我网站上的示例会强制用户在再次访问该网站时允许访问。

使用示例时,我犯了任何错误吗?我该怎么做才能避免一遍又一遍的权限请求?

提前感谢您的任何帮助!

4

3 回答 3

1

这对我来说很好。根据考沙尔的回答:

<?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
?>
于 2013-07-03T14:20:55.993 回答
1

首次使用此代码检索 access_code 并将其保存到数据库:

<?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>';
        } 
    ?>
于 2013-06-14T08:03:48.607 回答
0

Google Drive SDK 文档包含一个完整的 PHP 示例应用程序,您可以将其用作入门参考:

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

基本上,一旦用户登录并且您检索访问令牌和刷新令牌,您将这些凭据存储在数据库中并重复使用它们,而不是每次都要求用户进行身份验证。

于 2012-08-10T16:32:29.487 回答