0

I am trying to connect my web application with Google Drive, using the Google Drive API V2 and Google's PHP client library ("google/apiclient": "^2.0").

My application works fine for first hour. I can get files, uploads files, etc. but after 1 hour (when the access token expires and needs to be refreshed), subsequent gets/uploads result in a Google_Service_Exception (Err 400):

Your client has issued a malformed or illegal request.

enter image description here

I successfully obtain access & refresh tokens:

{
 "access_token":"**********",
 "expires_in":3600,
 "refresh_token":"1/********",
 "scope":"https://www.googleapis.com/auth/drive",
 "token_type":"Bearer",
 "created":1539085127
}

I have tried to refresh the token this way:

if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
}

And this way:

if ($client->isAccessTokenExpired()) {
    $client->refreshToken($client->getRefreshToken());
}

How can I properly maintain the credentials I received for more than an hour?

Mhrishad
  • 246
  • 1
  • 18
Mahidul Islam
  • 580
  • 11
  • 29

2 Answers2

0

Sounds like something is wrong with your client setup. Try this

function getOauth2Client() {
    try {

        $client = buildClient();

        // Set the refresh token on the client. 
        if (isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
            $client->refreshToken($_SESSION['refresh_token']);
        }

        // If the user has already authorized this app then get an access token
        // else redirect to ask the user to authorize access to Google Analytics.
        if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {

            // Set the access token on the client.
            $client->setAccessToken($_SESSION['access_token']);                 

            // Refresh the access token if it's expired.
            if ($client->isAccessTokenExpired()) {              
                $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
                $client->setAccessToken($client->getAccessToken()); 
                $_SESSION['access_token'] = $client->getAccessToken();              
            }           
            return $client; 
        } else {
            // We do not have access request access.
            header('Location: ' . filter_var( $client->getRedirectUri(), FILTER_SANITIZE_URL));
        }
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}

Ripped from Oauth2Authentication.php might help as well oauth2callback.php

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
0

You can try to get file list by using curl.

if you want to get all file from google drive.

Use This Url in curl:

https://www.googleapis.com/drive/v2/files

Or, If you want to get file from specific folder.

Use This Url in curl:

https://www.googleapis.com/drive/v2/files?q='".$yourDriveFolderId."'+in+parents

Google Drive Documentation

Mahidul Islam
  • 580
  • 11
  • 29
Delwar Sumon
  • 472
  • 2
  • 15