0

I am working on google drive API to create spreadsheet. First time when API authenticate with my gmail account it return me following information:

{
"access_token":"XXXXXXXXXs7ZqxIslxBnkLXXXXXJOvMjULXXXXXXXXXXX",
"expires_in":3600,
"scope":"https:\/\/www.googleapis.com\/auth\/spreadsheets",
"token_type":"Bearer",
"created":1536680902
}

Basically, it allow me to use access token only for 1 hour, after expiration of access token when I try to refresh token it's giving me below error:

Fatal error: Uncaught exception 'LogicException' with message 'refresh token must be passed in or set as part of setAccessToken' in C:\xampp\htdocs\drivegoogle\src\Google\Client.php:267

Below are my code that I am running to get access and refresh token:

function getClient() {


$client = new Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
// $client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY);
$client->setAuthConfig('credentials.json');
$client->setRedirectUri("http://localhost/drivegoogle/index.php");
$client->setAccessType('offline');
$client->setApprovalPrompt('force');

// Load previously authorized credentials from a file.
if (file_exists('1536680902.json')) {
$accessToken = json_decode(file_get_contents('1536680902.json'),
true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));

if (isset($_GET['code'])) {
$authCode = $_GET['code'];
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
header('Location: ' . filter_var($this->redirectUri,
FILTER_SANITIZE_URL));
if(!file_exists(dirname('1536680902.json'))) {
mkdir(dirname('1536680902.json'), 0700, true);
}

file_put_contents($this->tokenFile, json_encode($accessToken));
}else{
exit('No code found');
}
}

$client->setAccessToken($accessToken);

// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {

// save refresh token to some variable

$refreshTokenSaved = $client->getRefreshToken();
// update access token
$client->fetchAccessTokenWithRefreshToken($refreshTokenSaved);

// pass access token to some variable
$accessTokenUpdated = $client->getAccessToken();

// append refresh token
$accessTokenUpdated['refresh_token'] = $refreshTokenSaved;

//Set the new acces token
$accessToken = $refreshTokenSaved;
$client->setAccessToken($accessToken);

// save to file
file_put_contents($this->tokenFile,
json_encode($accessTokenUpdated));
}
}

Any body please help me to sort out problem. Thanks

stackers
  • 385
  • 3
  • 17
  • Hi, Refer below link, https://stackoverflow.com/questions/9241213/how-to-refresh-token-with-google-api-client It will helpful for you. – Raja C Sep 12 '18 at 10:22

1 Answers1

0
Try with Below Code,

function getClient() {

$client = new Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
// $client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY);
$client->setAuthConfig('credentials.json');
$client->setRedirectUri("http://localhost/drivegoogle/index.php");
$client->setAccessType('offline');
$client->setApprovalPrompt('force');

// Load previously authorized credentials from a file.
if (file_exists('1536680902.json')) {
$accessToken = json_decode(file_get_contents('1536680902.json'),
true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));

if (isset($_GET['code'])) {
$authCode = $_GET['code'];
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
header('Location: ' . filter_var($this->redirectUri,
FILTER_SANITIZE_URL));
if(!file_exists(dirname('1536680902.json'))) {
mkdir(dirname('1536680902.json'), 0700, true);
}

file_put_contents($this->tokenFile, json_encode($accessToken));
}else{
exit('No code found');
}
}

$accessToken = $client->setAccessToken($accessToken);
$decoded_accessToken= json_decode($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {

// save refresh token to some variable

$refreshTokenSaved = $client->getRefreshToken($decoded_accessToken->refresh_token);
// update access token
$client->fetchAccessTokenWithRefreshToken($refreshTokenSaved);

// pass access token to some variable
$accessTokenUpdated = $client->getAccessToken();

// append refresh token
$accessTokenUpdated['refresh_token'] = $refreshTokenSaved;

//Set the new acces token
$accessToken = $refreshTokenSaved;
$client->setAccessToken($accessToken);

// save to file
file_put_contents($this->tokenFile,
json_encode($accessTokenUpdated));
}
}
Raja C
  • 119
  • 11
  • Hi @Raja C, This piece of code is return nothing ( $accessToken = $client->setAccessToken($accessToken); $decoded_accessToken= json_decode($accessToken); print_r($decoded_accessToken); ) – stackers Sep 12 '18 at 11:41