PHP Google sheets quickstart

Viewed 123

I would like to export data to google sheets, I downloaded quickstart.php script from https://developers.google.com/sheets/api/quickstart/php I launched script from cli, it asks a "Enter verification code:" and give me a url to open with browser, I allow permission and redirect to a script I have to my server which display array it receives:

[access_token] => ....

[expires_in] => 3599
[refresh_token] => ...
[scope] => ....
[token_type] => Bearer
[created] => 1589307422

I paste access_token to cli asking code but it returns:

PHP Fatal error:  Uncaught InvalidArgumentException: Invalid token format in /var/www/html/vendor/google/apiclient/src/Google/Client.php:449
Stack trace:
#0 /var/www/html/quickstart.php(45): Google_Client->setAccessToken(Array)
#1 /var/www/html/quickstart.php(63): getClient()
#2 {main}
  thrown in /var/www/html//vendor/google/apiclient/src/Google/Client.php on line 449

What is wrong ?

2 Answers

I had something similar I kind of did this:

Replace (temporarily)

$authCode = trim(fgets(STDIN));

With:

$authCode = '4/yQFYBDGKFDSD54546Qhg6BZlT_gCEFZ7ixATB7657oc';

Because I had that auth code somewhere...

Hope you can do that too to get it started, Surely not the best way to do it, but that's what I did.

Solution

The code it's asking is not the access token. The access token will be actually retrieved by this authorization flow.

It's actually as easy as following the tutorial:

Once you get the quickstart up and running, it will prompt you to authorize access:

Browse to the provided URL in your web browser.

If you are not already logged into your Google account, you will be prompted to log in. If you are logged into multiple Google accounts, you will be asked to select one account to use for the authorization.

Click the Accept button.

Copy the code you're given, paste it into the command-line prompt, and press Enter.

Reference

PHP quickstart

Related