Skip to content
Advertisement

How to fetch auth token for service account using google API?

I have a WordPress site (running PHP) that needs to display its own analytics data to visitors using the google-api. I want to create the charts in Javascript, so I need to fetch an auth token from the PHP code and then pass that to the Javascript code. I can’t figure out how to get the auth token.

So far, I have code similar to this working using my service account: https://github.com/google/google-api-php-client/blob/master/examples/service-account.php

My code:

JavaScript

This all works, and I’m able to connect to Google_Service_Analytics and fetch analytics data. However, I can’t figure out how to fetch a service access token using my credentials, that I can hand off to the Javascript code so I can use the Google Analytics API from Javascript.

This doesn’t work:

JavaScript

Token just ends up being null. What do I need to do to fetch a token?

Advertisement

Answer

Here is the working code for me. The service account has a different type of process for generating access token. It is not following oauth client api.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement