Search code examples
phpgoogle-cloud-platformgoogle-cloud-functionsgcloud

Php equivalent for gcloud auth print-identity-token


My goal is to trigger a google cloud function through my php code. For that I need an identity token as the Bearer, but my server does not have gcloud installed on it. I have a working service-account and I created an object through the Google_Client ("google/apiclient": "2.10.1"):

$this->setAuthConfig($serviceJson);
$scope = [Google_Service_Compute::CLOUD_PLATFORM, 'openid', 'email'];
$this->addScope($scope);
if ($this->isAccessTokenExpired()) {
  $this->fetchAccessTokenWithAssertion();
}
$this->getAccessToken()

Using this client I can fetch only the access_token (which is unauthorized to trigger a cloud function) but the id_token is always empty.

How can I get the id_token? Is there a php library that can help or do I need to do a separate POST request(s) to get the id_token?


Solution

  • The only way I found is like this:

    1. Generate a JWT manually (from the data that is in the service account)
    2. send it to https://www.googleapis.com/oauth2/v4/token and receive a token
    3. use the token in the CF call