I am trying to use this API https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/ , but I don't know how to perform step 3 of it. The example is for python and I want to using this Client Library APIs for PHP. I need to have the access token using the JSON file provided.
Hi,
This example demonstrates how to authenticate using a service account and Google API PHP Client.
I am basically trying to make this example of the Python library in the PHP library. I wonder if it is possible to get this access token this way and how could it do it?
sudo pip install --upgrade google-api-python-client
# service-account.py
from oauth2client.service_account import ServiceAccountCredentials
# The scope for the OAuth2 request.
SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'
# The location of the key file with the key data.
KEY_FILEPATH = 'path/to/json-key.json'
# Defines a method to get an access token from the ServiceAccount object.
def get_access_token():
return ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILEPATH, SCOPE).get_access_token().access_token
Either of the following will return an access token.
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json');
$scope = 'https://www.googleapis.com/auth/analytics.readonly';
$client = new Google_Client();
$client->setScopes($scope);
$client->useApplicationDefaultCredentials();
print_r($client->fetchAccessTokenWithAssertion());
use Google\Auth\CredentialsLoader;
$scope = 'https://www.googleapis.com/auth/analytics.readonly';
$credentials = CredentialsLoader::makeCredentials($scope, json_decode(file_get_contents(
'/path/to/keyfile.json'
), true));
print_r($credentials->fetchAuthToken());
If you are not using the API Client for anything besides obtaining an access token, I suggest the latter, as it only requires installation of google/auth, allowing you to drop the much larger dependency on google/apiclient.
It worked, after a week trying to make it work. It was a simple method. Thanks so much for the help, even in stackoverflow they couldn't help me.
Glad to hear it! Sorry I didn't fully understand the request at first.
It is because I do not have much skill with English, but thank you very much for the help, I thought I would have no more solution, I tried several methods.
Most helpful comment
Either of the following will return an access token.
If you are not using the API Client for anything besides obtaining an access token, I suggest the latter, as it only requires installation of
google/auth, allowing you to drop the much larger dependency ongoogle/apiclient.