Google-api-php-client: How to get OAuth 2.0 access token for Firebase Cloud Messaging HTTP v1 API

Created on 17 Sep 2019  路  2Comments  路  Source: googleapis/google-api-php-client

The FCM HTTP V1 API requires the server to get a "short-lived OAuth 2.0 access token" to use as the Authorization: Bearer=TOKEN request header. To do so, the docs simply point in the direction of this API Client Library, and they show some examples for the Node and other non-PHP languages:

image

After exploring the present google-api-php-client library, I can't find any possible way to get this token. There are mentions to OAuth 2.0 and tokens in many places, but there isn't a simple example or explanation on how to get an OAuth 2.0 access token for use with FCM HTTP V1 requests.. That is all I need to get to continue with my own PHP-based curl() implementation of the FCM API.

If google wants developers to migrate to the HTTP v1 API, this process should be clearly explained in a simple example in the very home page of the PHP client library.

Does anyone know how to get such token with this library? Is it even possible? Thanks in advance.

question

Most helpful comment

You may use either of the following methods to obtain 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.

If you wish to call Firebase Cloud Messaging via the PHP API Client, you can allow the client to authenticate on your behalf similarly to the first example:

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();

$fcm = new Google_Service_FirebaseCloudMessaging($client);
// construct and execute API requests, will be authenticated using the service account.

All 2 comments

You may use either of the following methods to obtain 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.

If you wish to call Firebase Cloud Messaging via the PHP API Client, you can allow the client to authenticate on your behalf similarly to the first example:

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();

$fcm = new Google_Service_FirebaseCloudMessaging($client);
// construct and execute API requests, will be authenticated using the service account.

I'm going to close this issue out. If you're still having trouble, please feel free to re-open!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mevsme picture mevsme  路  4Comments

AlexandreGerault picture AlexandreGerault  路  5Comments

ghost picture ghost  路  4Comments

artemiusgreat picture artemiusgreat  路  3Comments

slaFFik picture slaFFik  路  5Comments