Google Drive API with PHP - Problems updating the token

0

I am trying to update the token in the PHP code but it is not working. I am using the Google Drive API with PHP, the code works well in the browser until the token expires, checking in other pages I have changed the code in this way:

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

 //También lo he intentado de esta forma pero tampoco funciona:
 //$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));
}
?>

But I still get the error message in the logs:

  

PHP Fatal error: Uncaught exception 'LogicException' with message 'refresh token must be passed in or set as part of setAccessToken' in /var/www/drive/vendor/google/apiclient/src/Google/Client.php: 266 \ nStack trace: \ n # 0 /var/www/drive/index.php(14): Google_Client-> fetchAccessTokenWithRefreshToken (NULL) \ n # 1 {main} \ n thrown in / var / www / drive / vendor /google/apiclient/src/Google/Client.php on line 266

What I have to do is manually remove the credentials.json file that stores the expired token and generate a new one by running index.php and being redirected oauth2callback .php every hour, but that's not the idea.

How can I correct it?

I would like to receive your help.

    
asked by NekoLopez 19.09.2018 в 17:35
source

1 answer

1

In order for the refresh token to work, you must specify the setAccessType () of the code block:

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


 // offline: Necesario para renovar token de acceso a GDrive
 $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'] . '/';
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
 }

? >

NOTE: I recommend you delete your client_id file and generate a new one before generating the token to avoid problems.

    
answered by 19.09.2018 / 18:01
source