Google Cloud Authorized HTTP requests in Python

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-10 14:29:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!