【问题标题】:Google Cloud Authorized HTTP requests in PythonPython 中的 Google Cloud 授权 HTTP 请求
【发布时间】:2020-08-17 19:19:26
【问题描述】:

我正在尝试使用以下代码从 Python 中向 Google Cloud 函数发出授权请求:

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)

但我收到以下错误:

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."}') 

这让我觉得我的凭据设置不正确,但如果我使用以下命令从命令行运行它,调用就可以工作:

gcloud functions call MY_CLOUD_FUNCTION --data '{"name":"example"}' 

有人知道我为什么会收到这个错误吗?任何帮助将不胜感激!

【问题讨论】:

  • 你在哪里运行调用函数的 Python 脚本?

标签: python google-cloud-platform python-requests google-cloud-functions


【解决方案1】:

这里的答案取决于您尝试从哪里调用函数。

如果它位于 GCP 基础架构(App Engine、Cloud Functions 等)中,您应该使用"Authenticating Developers, Functions, and End-users" 的"Function-to-functon" 部分中的步骤。

如果是外部的,您应该遵循 "Service-to-function" 指南。

【讨论】:

    猜你喜欢
    • 2021-04-26
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 2021-04-04
    • 2015-03-08
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多