php - 使用带有php脚本的google drive api自动刷新 token

标签 php google-api google-drive-api google-api-php-client

我又关注了THIS TUTORIAL直接从我的远程服务器使用 php 将文件上传到 Google Drive:所以我从 Google API 控制台创建了新的 API 项目,启用了 Drive API 服务,请求了 OAuth 客户端 ID 和客户端密码,将它们写在脚本中,然后上传它连同 Google APIs Client Library for PHP文件夹到这个 http://www.MYSERVER.com/script1.php , 检索授权码:

<?php

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

$drive = new Google_Client();

$drive->setClientId('XXX'); // HERE I WRITE MY Client ID

$drive->setClientSecret('XXX'); // HERE I WRITE MY Client Secret

$drive->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');

$drive->setScopes(array('https://www.googleapis.com/auth/drive'));

$gdrive = new Google_DriveService($drive);

$url = $drive->createAuthUrl();
$authorizationCode = trim(fgets(STDIN));

$token = $drive->authenticate($authorizationCode);

?>

当我访问http://www.MYSERVER.com/script1.php时我允许授权并获得我可以在第二个脚本中编写的授权代码。然后我上传到http://www.MYSERVER.com/script2.php , 谁看起来像:

<?php

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

$drive = new Google_Client();

$drive->setClientId('X');  // HERE I WRITE MY Client ID
$drive->setClientSecret('X');  // HERE I WRITE MY Client Secret
$drive->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$drive->setScopes(array('https://www.googleapis.com/auth/drive'));

$gdrive = new Google_DriveService($drive);

$_GET['code']= 'X/XXX'; // HERE I WRITE AUTH CODE RETRIEVED AFTER RUNNING REMOTE script.php

file_put_contents('token.json', $drive->authenticate());

$drive->setAccessToken(file_get_contents('token.json'));

$doc = new Google_DriveFile();

$doc->setTitle('Test Drive');
$doc->setDescription('Document');
$doc->setMimeType('text/plain');

$content = file_get_contents('drive.txt');

$output = $gdrive->files->insert($doc, array(
      'data' => $content,
      'mimeType' => 'text/plain',
    ));

print_r($output);

?>

好吧,现在文件 drive.txt 已上传到我的 Google 云端硬盘,token.json 文件的结构如下:

{"access_token":"XXX","token_type":"Bearer","expires_in":3600,"refresh_token":"YYY","created":1365505148}

现在,正如您想象的那样,我可以调用 script2.php 并上传文件直到某个时间。最后,要点是:我不想 token 过期,我不想在每次过期时都允许授权(回顾 script1.php):我需要在白天定期调用 script2.php,以自动上传我的文件,无需用户交互。那么,在此上下文中永远自动刷新 token 的最佳方法是什么?我需要另一个脚本吗?我可以在 script2.php 中添加一些代码吗?或者修改 token.json 文件?我在哪里可以读取 token 过期前的剩余时间?谢谢!

最佳答案

您不必定期请求访问 token 。如果您有 refresh_token,PHP 客户端会自动为您获取一个新的访问 token 。

为了获取 refresh_token,您需要将 access_type 设置为“offline”并请求离线访问权限:

$drive->setAccessType('offline');

一旦你得到一个代码

$_GET['code']= 'X/XXX';
$drive->authenticate();

// persist refresh token encrypted
$refreshToken = $drive->getAccessToken()["refreshToken"];

对于 future 的请求,请确保始终设置刷新 token :

$tokens = $drive->getAccessToken();
$tokens["refreshToken"] = $refreshToken;
$drive->setAccessToken(tokens);

如果你想强制访问 token 刷新,你可以通过调用 refreshToken 来实现:

$drive->refreshToken($refreshToken);

请注意,refresh_token 只会在第一个 $drive->authenticate() 时返回,您需要永久存储它。为了获得新的 refresh_token,您需要撤销现有 token 并重新启动身份验证过程。

离线访问在Google's OAuth 2.0 documentation上有详细解释.

关于php - 使用带有php脚本的google drive api自动刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15905104/

相关文章:

javascript - 如何使用 google drive api 使文件可共享

google-apps-script - 与其他用户共享 Google App 脚本 - 请求权限并授权脚本

google-apps-script - 有没有办法为 Google Drive 文件夹中的所有视频添加水印?

PHP问号

google-api - Google people.people.connections.list 分页似乎已损坏

php - LinkedIn API - 验证用户是否已在服务器端登录

ios - 使用 Google Vision API 发出请求 - 收到无效的 JSON 有效负载且 INVALID_ARGUMENT

c# - 如何在 Windows Phone 上使用 Auth Token 获取 Google Tasks (REST API)

php - 使用 Post 数据重定向 URL

php - 如何防止 PHP 中的 SQL 注入(inject)?