我的目标是通过我的 php 代码触发谷歌云功能。为此,我需要一个身份 token 作为承载者,但我的服务器上没有安装 gcloud。 我有一个可用的服务帐户,并通过 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()
使用此客户端,我只能获取 access_token(未授权触发云功能),但 id_token 始终为空。
如何获取 id_token?是否有一个 php 库可以提供帮助,或者我是否需要执行单独的 POST 请求来获取 id_token?
最佳答案
我发现的唯一方法是这样的:
- 生成 JWT manually (来自服务帐户中的数据)
- 发送至https://www.googleapis.com/oauth2/v4/token并接收 token
- 在 CF 调用中使用 token
关于gcloud auth print-identity-token 的 PHP 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70532731/