Google Drive API oauth2 hangs on live server and returns no error when authenticating with auth_code

281 views Asked by At

I have a huge problem where absolutely no error is returned from google nor php.

I've written a google client oauth2 connect model, which is perfectly working when I'm running it on my dev environment on my mac (I'm not using a simple localhost, it's actually vagrant what I'm using to mirror the production server as much as possible) but it doesn't work when I push it to production.

It's just simply not returning any error. The code reaches the part where I have the auth code from Google and it hangs at the part where I'm trying to exchange it for a token ( $client->authenticate($auth_code) ).

I really don't know what am I doing wrong, since everything is working perfect without errors on dev site (which has a public link by the way, and I tried accessing it from different public IP addresses).

The moment I push the code to a live server, it just stops authenticating. I have the correct oauth credentials set up for every production server, redirect uri is correct. I simply don't know what am I doing wrong...

Here's a part of code (I repeat, it perfectly works on dev server with publicly accessible url):

$this->client = new Google_Client();
$this->credentials_path . '/client_secret.json'
$this->client->setAuthConfigFile($this->credentials_path . '/client_secret.json');
$this->client->setAccessType('offline');
$this->client->setRedirectUri($this->redirect_uri);
$this->client->addScope(Google_Service_Drive::DRIVE_READONLY);
$this->client->authenticate($auth_code); //this is the part where the code hangs on a live server, but works perfectly on dev
$this->token = json_encode($this->client->getAccessToken());
$this->client->setAccessToken($this->token);
$this->writeTokenToFile($this->token);

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

$google_drive_service = new Google_Service_Drive($this->client);

Any help would be very much appreciated!

1

There are 1 answers

0
Teyam On

AFAIK, you need to revoke the existing token before requesting another.

As mentioned in Offline access, when you set the API client's access type to offline, the client object will refresh the access token as needed.

With that, you need to revoke access given to an application. As mentioned,

part of the removal process can include an API request to ensure the permissions granted to the application are removed.

You may want to try revoking a token by calling revokeToken():

$client->revokeToken();

You may want to also check given solutions in the following SO posts:

Hope that helps!