Gmail API: How to get access token?

16,084

Solution 1

Basically I was approaching wrongly. Following these instructions is enough to get the tokens:

https://developers.google.com/gmail/api/quickstart/php

The main point is to access the file from the command line and not from the app.

Solution 2

The next step in the flow is to exchange the Authorization Code for an Access Token (which will also include a Refresh Token if you requested offline access). If you use the https://developers.google.com/oauthplayground/ to execute the flow manually, you'll be able to see the URLs involved. There is a php library call to do the same thing, but I personally prefer to send my own HTTP rather than use a library. Even if you do use a library, it will still be worth spending a little time to understand the HTTP flow so you can more easily debug any problems you encounter.

Share:
16,084
FrancescoMussi
Author by

FrancescoMussi

Full-stack developer based in Riga, Latvia. Hope Socrates is proud of my Socratic badge on StackOverflow.

Updated on June 04, 2022

Comments

  • FrancescoMussi
    FrancescoMussi almost 2 years

    I am testing the Gmail API.

    So far I have done the following:

    So now when I run the file quickstart.php it gives a link. When I open it, it appears an authorization page where I authorize my application to access the Gmail API.

    Then it redirects to the Redirect URIs that I have declared in the setup (adding the code parameter).

    In the address bar it appears exactly this:

    http://localhost/main/gmail_callback?code=MY_CODE

    Where main is my controller and gmail_callback so far is just a blank function.

    And it should be correct since these are my settings:

    • Javascript origins: http://localhost
    • Redirect URIs: http://localhost/main/gmail_callback

    What do I do next?