问题
I am trying to make an authorized request to a Google Cloud function from within Python using this code:
import google.auth
from google.auth.transport.urllib3 import AuthorizedHttp
credentials, project = google.auth.default()
authed_http = AuthorizedHttp(credentials)
url = "MY_CLOUD_FUNCTON_URL"
response = authed_http.request('POST', url)
but I am getting the following error:
google.auth.exceptions.RefreshError: ('invalid_scope: Invalid OAuth scope or ID token audience provided.', '{"error":"invalid_scope","error_description":"Invalid OAuth scope or ID token audience provided."}')
which makes me think my credentials aren't setup properly but the call works if I run it from the command line using:
gcloud functions call MY_CLOUD_FUNCTION --data '{"name":"example"}'
Does anyone know why I am getting this error? Any help would be much appreciated!
回答1:
The answer here depends on where you're trying to call the function from.
If it's inside GCP infrastructure (App Engine, Cloud Functions, etc) you should use the steps in the "Function-to-functon" section of "Authenticating Developers, Functions, and End-users".
If it's external, you should follow the "Service-to-function" guide instead.
来源:https://stackoverflow.com/questions/61574031/google-cloud-authorized-http-requests-in-python