In order to authenticate with Cloud Endpoints for OpenAPI, I have to construct a Python requests session using google-auth as follows:
from google.auth.transport.requests import AuthorizedSession
creds = google.auth.jwt.Credentials.from_service_account_file(
creds_path, audience=my_audience)
session = AuthorizedSession(creds)
But when I want to authenticate to a Cloud Function, I have to do it a little different:
creds = google.oauth2.service_account.IDTokenCredentials.from_service_account_file(creds_path, target_audience=function_url)
session = AuthorizedSession(creds)
And all that is when I use a service account file, such as when running from my local machine, or on GKE. But when it's used on App Engine, there's another variant:
Calling Cloud Endpoints -
boostrap_creds, _ = google.auth.default()
creds = google.auth.jwt.Credentials.from_signing_credentials(boostrap_creds, my_audience)
session = AuthorizedSession(creds)
Calling Cloud Function --
IAM_SCOPE = 'https://www.googleapis.com/auth/iam'
OAUTH_TOKEN_URI = 'https://www.googleapis.com/oauth2/v4/token'
bootstrap_credentials, _ = google.auth.default(scopes=[IAM_SCOPE])
signer_email = bootstrap_credentials.service_account_email
signer = bootstrap_credentials.signer
creds = google.oauth2.service_account.IDTokenCredentials(
signer, signer_email, token_uri=OAUTH_TOKEN_URI, target_audience=function_url)
session = AuthorizedSession(creds)
Why is there a difference, and what does it mean?