Connect to Google Cloud services

You can use the ScriptApp.getIdentityToken() method to get an OpenID Connect identity token (a JSON Web Token or JWT) for the effective user. You can use this token to authenticate with Google Cloud services, such as Cloud Run, that are configured to accept it.

Enable the openid scope

The openid scope is required to generate an OpenID Connect ID token. You must also list any other scopes your script uses, such as https://www.googleapis.com/auth/script.external_request for the UrlFetch service. The https://www.googleapis.com/auth/userinfo.email scope is included in this example to add the user's email address to the identity token.

In your script project's manifest file (appsscript.json), add the openid scope and any other required scopes to the oauthScopes array:

{
"timeZone":"America/New_York",
"dependencies":{
},
"exceptionLogging":"STACKDRIVER",
"runtimeVersion":"V8",
"oauthScopes":[
"openid",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/userinfo.email"
]
}

Configure the Google Cloud service

You must configure the Google Cloud service to accept the identity token issued to your script. This typically involves adding the script's client ID as an allowed audience.

To find your script's client ID, you can decode an identity token:

functionlogClientId(){
constidToken=ScriptApp.getIdentityToken();
constbody=idToken.split('.')[1];
constdecoded=Utilities.newBlob(Utilities.base64Decode(body)).getDataAsString();
constpayload=JSON.parse(decoded);
Logger.log('Client ID: '+payload.aud);
}

For Cloud Run, you can configure custom audiences to allow this client ID.

Make an authenticated request

Once configured, you can include the identity token in the Authorization header of your requests:

functioncallCloudRunService(){
constidToken=ScriptApp.getIdentityToken();
consturl='https://your-service-url.a.run.app';
constresponse=UrlFetchApp.fetch(url,{
headers:{
'Authorization':'Bearer '+idToken
}
});
Logger.log(response.getContentText());
}

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026年07月22日 UTC.