使用带有 php 脚本的 google drive api 自动刷新令牌 [英] Automatically refresh token using google drive api with php script

查看:34
本文介绍了使用带有 php 脚本的 google drive api 自动刷新令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再次关注本教程上传使用 php 在 Google Drive 上的一个文件,直接来自我的远程服务器:所以我从 Google API 控制台创建了新的 API 项目,启用了 Drive API 服务,请求 OAuth 客户端 ID 和客户端密钥,将它们写入脚本,然后将其上传PHP 的 Google API 客户端库 文件夹到此 http://www.MYSERVER.com/script1.php,获取验证码:

setClientId('XXX');//这里我写我的客户 ID$drive->setClientSecret('XXX');//这里我写了我的客户端密码$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,谁看起来喜欢:

setClientId('X');//这里我写我的客户 ID$drive->setClientSecret('X');//这里我写了我的客户端密码$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';//这里我写了运行远程脚本后检索到的授权代码.file_put_contents('token.json', $drive->authenticate());$drive->setAccessToken(file_get_contents('token.json'));$doc = new Google_DriveFile();$doc->setTitle('试驾');$doc->setDescription('文档');$doc->setMimeType('text/plain');$content = file_get_contents('drive.txt');$output = $gdrive->files->insert($doc, array('数据' =>$内容,'mimeType' =>'文本/普通',));打印_r($输出);?>

好吧,现在文件 drive.txt 已上传到我的 Google Drive 并且 token.json 文件的结构是这样的:

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

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

解决方案

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

为了检索一个refresh_token,你需要将access_type设置为offline"并请求离线访问权限:

$drive->setAccessType('offline');

一旦你得到一个code

$_GET['code']='X/XXX';$drive->authenticate();//持久化刷新令牌加密$refreshToken = $drive->getAccessToken()["refreshToken"];

对于以后的请求,请确保始终设置刷新的令牌:

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

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

$drive->refreshToken($refreshToken);

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

离线访问在 Google 的 OAuth 2.0 文档中有详细说明.>

I followed again THIS TUTORIAL to upload a file on Google Drive with php, directly from my REMOTE SERVER: so I have created new API Project from Google API Console, enabled Drive API service, requested OAuth Client ID and Client Secret, wrote them in a script, then upload it together with Google APIs Client Library for PHP folder to this http://www.MYSERVER.com/script1.php, to retrieve Auth code:

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

?>

When I visit http://www.MYSERVER.com/script1.php I allow authorization and get the Auth code that I can write in a second script. Then I upload it to http://www.MYSERVER.com/script2.php, who looks like:

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

?>

Well, now the file drive.txt is uploaded on my Google Drive and structure of token.json file is a sort of:

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

Now, as you can imagine I can call script2.php and upload file until a certain time. Finally, the point is: I don't want the token to expire, I don't want to allow authorization each time it expire (recalling script1.php): I need to call the script2.php periodically during the day, to upload my file automatically, without user interaction. So, what's the best way to automatically refresh the token forever in this context? Do I need another script? Can I add some code to script2.php? or modify the token.json file? And where can I read the time remaining before the token expire? Thanks!

解决方案

You don't have to periodically ask for an access token. If you have a refresh_token, PHP client will automatically acquire a new access token for you.

In order to retrieve an refresh_token, you need to set access_type to "offline" and ask for offline access permissions:

$drive->setAccessType('offline');

Once you get a code,

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

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

For future requests, make sure that refreshed token is always set:

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

If you want a force access token refresh, you can do it by calling refreshToken:

$drive->refreshToken($refreshToken);

Beware, refresh_token will be returned only on the first $drive->authenticate(), you need to permanently store it. In order to get a new refresh_token, you need to revoke your existing token and start auth process again.

Offline access is explained in detail on Google's OAuth 2.0 documentation.

这篇关于使用带有 php 脚本的 google drive api 自动刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆