【发布时间】:2014-02-18 17:38:05
【问题描述】:
我按照谷歌驱动器上的教程进行操作,它说要保留身份验证/访问令牌,我们必须使用刷新令牌,这样我们就不必在每次进行 API 调用时都要求用户进行身份验证/授权.
代码:
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 console
$client->setClientId('client ID');
$client->setClientSecret('client secret');
$client->setRedirectUri('URL');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$client->setAccessType('offline');
$service = new Google_DriveService($client);
$authUrl = $client->createAuthUrl();
// Exchange authorization code for access token
$accessToken = $client->authenticate();
$client->setAccessToken($accessToken);
$files = $service->files->listFiles();
echo "<pre>"; print_r($files);
这给了我文件和文件夹的列表,但每次我刷新页面时,它都会带我回到谷歌授权页面。我可以将访问令牌保存在数据库中,但是如何使用它并进行 API 调用而无需再次向用户请求授权??
有什么想法吗??
谢谢,
阿尼克特
【问题讨论】:
标签: php google-drive-api