【问题标题】:Google Drive API with PHP - Problems with refresh token getting message 'refresh token must be passed in or set as part of setAccessToken'带有 PHP 的 Google Drive API - 刷新令牌获取消息“刷新令牌必须传入或设置为 setAccessToken 的一部分”的问题
【发布时间】:2019-02-23 01:14:06
【问题描述】:

我正在尝试刷新我的 PHP 脚本上的令牌,但它还没有工作。我正在使用带有 PHP 的 Google Drive API,我的脚本在浏览器中运行良好,直到令牌过期,检查其他网站我以这种方式更改了代码:

index.php

<?php
require_once 'vendor/autoload.php';
if (!file_exists("client_id.json")) exit("Client secret file not found");
$client = new Google_Client();
$client->setAuthConfig('client_id.json');
$client->addScope(Google_Service_Drive::DRIVE);

if (file_exists("credentials.json")) {
$access_token = (file_get_contents("credentials.json"));
$client->setAccessToken($access_token);
//Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
    $refreshTokenSaved = $client->getRefreshToken(); 
    $client->fetchAccessTokenWithRefreshToken($refreshTokenSaved);
    $accessTokenUpdated = $client->getAccessToken();
    $accessTokenUpdated['refresh_token'] = $refreshTokenSaved;
    file_put_contents("credentials.json", json_encode($accessTokenUpdated));

     //I also tried in this way but it's also not working:
    //$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    //file_put_contents("credentials.json", json_encode($client->getAccessToken()));
}

$drive_service = new Google_Service_Drive($client);
$files_list = $drive_service->files->listFiles(array())->getFiles(); //http://stackoverflow.com/questions/37975479/call-to-undefined-method-google-service-drive-filelistgetitems
echo json_encode($files_list);
} else {
  $redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/drive/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>

oauth2callback.php

<?php
require_once 'vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfigFile('client_id.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/drive/oauth2callback.php');
$client->addScope(Google_Service_Drive::DRIVE); //::DRIVE_METADATA_READONLY
if (! isset($_GET['code'])) {
  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
 $client->authenticate($_GET['code']);
 $access_token = $client->getAccessToken();
 file_put_contents("credentials.json", json_encode($access_token));

 $redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/';
 header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>

但我仍然在日志中收到消息:

PHP 致命错误:在 /var/www/drive/vendor/google/apiclient/src/Google/Client.php 中带有消息“必须传入或设置为 setAccessToken 的一部分”的未捕获异常“LogicException”: 266\n堆栈跟踪:\n#0 /var/www/drive/index.php(14): Google_Client->fetchAccessTokenWithRefreshToken(NULL)\n#1 {main}\n 抛出 /var/www/drive/vendor/ google/apiclient/src/Google/Client.php 第 266 行

我必须删除 credentials.json 并生成一个新的执行 index.php 并每小时重定向到 oauth2callback.php,但这不是想法。

我该如何解决?

我想得到你的帮助。

【问题讨论】:

  • 我认为SO post 可以帮助您解决问题。它还使用 PHP 使用了quickstart

标签: php google-api google-drive-api


【解决方案1】:

最终令牌刷新工作,我不得不在 oauth2callback.php 中添加这一行 $client-&gt;setAccessType("offline");,同时删除并生成一个新的 client_id.json 以避免错误。

<?php
 require_once 'vendor/autoload.php';
 $client = new Google_Client();
 $client->setAuthConfigFile('new_client_id.json');
 $client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/drive/resp.php');
 $client->addScope(Google_Service_Drive::DRIVE); //::DRIVE_METADATA_READONLY

 $client->setAccessType("offline");

 if (! isset($_GET['code'])) {
   $auth_url = $client->createAuthUrl();
   header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
 } else {
   $client->authenticate($_GET['code']);
   $access_token = $client->getAccessToken();
   file_put_contents("credentials.json", json_encode($access_token));

   $redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/drive';
   header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
 }
 ?>

【讨论】:

    猜你喜欢
    • 2021-08-02
    • 2017-01-11
    • 2018-02-24
    • 2019-02-16
    • 2020-12-11
    • 2017-10-10
    • 2012-02-15
    • 2014-07-15
    • 1970-01-01
    相关资源
    最近更新 更多