Google Cloud how to get ID Token by Service Account using php

Viewed 480

I created a Service Account, created key for it and stored as JSON file (which includes roles/cloudfunctions.invoker role), saved on local. And have some code which is trying to call Cloud Functions on GCP.

I tested successfully to call my Function on local via curl

curl -X POST  -H "Authorization: bearer $(gcloud auth print-identity-token)" -H "Content-Type:application/json" -d '{"data" : {"id" : "121123"}}' "https://asia-northeast1-my-project-name.cloudfunctions.net/MyFunction"

Now I want to call this function on my on-premise server, but don't want to install Cloud SDK which has 'gcloud' command (because gcloud auth require authenticate via browser). I copied above JSON file on local to this server and attempt to use Google APIs Client Library for PHP to generate ID Token which is equivalent to command gcloud auth print-identity-token. I also referred to this answer

require __DIR__ . '/vendor/autoload.php';

$client = new Google_Client();
$client->setAuthConfig('/path/to/json/file.json');
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/cloud-platform');
$client->fetchAccessTokenWithAssertion();
$access_token = $client->getAccessToken();
var_dump($access_token);

If I have a valid token here I can call my Cloud Function, but when var_dump this $access_token, it printed out a weird string (which have so much 'dot' at the end) - looks like this:

ab29.c.Kp4BDgaiwej12ckskW0waSinDiMRcayZoBm5tp1lvc2813uEZjraVbU-4hKXxG2euqNS9wxRUa1aEKOH7n5w_VAhIUW9821Cy7Ca0c7_sXVDr_Nwouj8sVZR6gnjUFx51n8azUTX-RFMJBGE4_MrPT3hIMYYeBlIxy_IEUHF932xKPWR0ulf-wOa0o...............................................................................................................................................................................................................................................................................................................................................................................................

Of course this access_token cannot be used.

What is wrong with my steps above? If this way cannot be done, can you suggest a way that allow me to call Cloud Functions on on-premise server?

0 Answers
Related