In a Laravel App I try to get permission to save files on a Google Drive. As part of calling the api one can pass query parameters that should be returned.
Like this:
$client = new Client([
'client_id' => config('services.google-drive.client_id'),
'client_secret' => config('services.google-drive.client_secret'),
'scopes' => [
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive',
],
'redirect_uri' => route('users.integrations.google-drive.connect'), // NOTE: I do add the parameters here
'approval_prompt' => 'force',
'access_type' => 'offline',
]);
$authUrl = $client->createAuthUrl($client->getScopes(), ['igd' => $idg]);
$authUrl is:
https://accounts.google.com/o/oauth2/v2/auth?response_type=code&access_type=offline&client_id=XXXXXXXX&redirect_uri=YYYYYY&state&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&approval_prompt=force&igd=99df36fb-ee4a-4861-8d57-85b0b2905bcd
The last parameter is from my app so I send it to Google.
After approving the request the ‘callback’ url looks like this:
https://<my_return_url>?code=4%2F0Adeu5BWppRGhaAsAwDlDxbAMhbFIaEqk1rSHNdMKuQcKe-JXwcTE8fZZmMY3xLSv8XSs7g&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file
The ‘igd’ parameter is missing.
Any hint why ?
Thanks.
Kind regards
Steen
2
Answers
Thanks for your answer, but it's not 100% correct.
Even with the above, one might not get the query parm back - for whatever reason
What I have found to be working is a call to
$client->setState('my_value')
then in my callback I check for the 'state' key in the request
I have also found out why you sometins don't get the refresh token, it is due to fact that you have already have some kind of connection.
If you do not get the refresh token, then you can go to the google account and look at connections and then manually disconnect or try to call the
$client->revokeToken()
Here is a few methods that works for me - my specific use case
NOTE: UserIntegrationGoogleDrive $integrationGoogleDrive is just a record in the db to store some information
you are missing a step you need to exchange the code for the access token