使用PHP脚本使用Google Drive api自动刷新令牌 [英] Automatically refresh token using google drive api with php script

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

问题描述

我继续此教程上传Google Drive上的一个文件,使用php,直接从我的REMOTE SERVER获得:所以我从Google API Console创建了一个新的API项目,启用了Drive API服务,请求了OAuth客户端ID和客户端密码,将它们写入脚本,然后一起上传 Google API客户端库PHP 文件夹至 http://www.MYSERVER.com/script1.php ,检索认证码:

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

?>

当我访问 http://www.MYSERVER.com/script1.php 允许授权并获取我可以在第二个脚本中写入的验证码。然后我将它上传到 http://www.MYSERVER.com/script2.php ,他的长相如:

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

?>

好了,现在将文件drive.txt上传到我的Google云端硬盘中,并将token.json文件是一种:

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}

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

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!

推荐答案

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

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.

为了检索refresh_token,您需要将access_type设置为offline并要求离线访问权限:

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

$drive->setAccessType('offline');

一旦得到代码, p>

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

如果您想要强制访问令牌刷新,可以通过调用 refreshToken

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

$drive->refreshToken($refreshToken);

请注意, refresh_token 第一个 $ drive-> authenticate(),您需要永久存储它。为了获得一个新的refresh_token,你需要撤消你现有的令牌并重新开始auth过程。

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.

离线访问在 Google的OAuth 2.0文档

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

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