Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!