我一直在尝试自己解决这个问题,但一直没能做到。 我制作了一个简单的云函数(第一代),它应该给出消息“hello world”
def hello_world(request):
return 'Hello World'
我将其命名为 function-test-3 并部署。 然后我创建了另一个函数,我想从中调用该函数并通过请求调用中的标头传递所需的授权参数。
import google.oauth2.id_token
import google.auth.transport.requests
import requests
endpoint = 'https://us-central1-sport-app-395020.cloudfunctions.net/function-test-3'
def make_authorized_get_request(endpoint):
auth_req = google.auth.transport.requests.Request()
id_token = google.oauth2.id_token.fetch_id_token(auth_req, endpoint)
header_1 = {'Authorization':'Bearer '+id_token}
response = requests.get(url=endpoint,headers=header_1)
return str(response.content)
然后我收到 401 错误。我不知道还能做什么,我已经为必须调用函数的服务帐户赋予了所有者、函数调用者、函数管理员和大量其他角色,无论是在被调用函数还是调用者函数中。这似乎并不能解决问题。
如果我通过 cli 打印 id 令牌并将字符串粘贴到请求的标头中,那么它就可以工作。
我希望它可以在没有 json 文件的情况下工作。因为我见过一些使用它的方法。
任何想法将不胜感激。