Google Analytics API oAuth – ever wondered how to get the ‘access_token’ ?

I’m sure that a lot of veteran Google Analytics Ninjas would be familiar with the Google Analytics Query Explorer.

This wonderful tool, allows you to create and compile URI calls relatively quickly, and in my humble opinion, I prefer it over using the Google Analytics interface to get some data.

The query explorer compiles an API Query URI that would look something like this:

Google Analytics API Query URI
Google Analytics API Query URI

The eagle-eyed analysts would have realised that the access_token expires generally around 60 minutes so this blog will teach you how to re-generate the access_token in 7 easy steps!

Most of the tokens in this example have been changed or disguised.


Part 1: Create an oAuth ID (Other)


  1. Go to:
  2. Create a new project
  3. Enable the “Analytics API”
    New project creation
    New project creation

    Go to credentials

  4. Create “oAuth Client ID”
  5. Configure (Other) and save
  6. Give it a name
  7. Save the client ID and client secret
  8. Download the JSON


Part 2: Compile the following URI call from the JSON 


Copy the client ID from the JSON into the URI to become:


Call the above URI.


Part 3: If done right… you will get this screen. Answer ALLOW! 

Google will then issue you with this token – it may only be used once.

Google Analytics API authorisation
Google Analytics API authorisation

Google oAuth Analytics API authorisation


Part 4: Open up Postman – or download it at 

Make a POST call to:

With the following parameters:


  • Content-Type: application/x-www-form-urlencoded


  • grant_type: authorization_code
  • code: 4/XP1d0cQSjFFW0GTvroHSKTtheZfX-lXhyeFEBE71uxQ (important code from before)
  • client_id: YOUR CLIENT ID from setup
  • client_secret: YOUR CLIENT SECRET from setup
  • redirect_uri: urn:ietf:wg:oauth:2.0:oob


Part 5: If done right, you will get a happy POSTMAN

Postman example
Postman example


Part 6: Use the access_token to access any Google Analytics API URI

For example, you may now call, and the data will come back in JSON format.


Google Analytics API output JSON example
Google Analytics API output JSON example

Part 7: You can refresh the token using the refresh_token

Tokens usually last for around 60 minutes so be sure to build an error handler.


For the lazy, below is what the code could look like. Of course, the implementation could be done in many other ways.




Leave a Reply