Google Cloud授权的Python HTTP请求 [英] Google Cloud Authorized HTTP requests in Python

查看:122
本文介绍了Google Cloud授权的Python HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

但是出现以下错误:

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!

推荐答案

此处的答案取决于您尝试从何处调用函数.

The answer here depends on where you're trying to call the function from.

如果它位于GCP基础架构(App Engine,云功能等)之内,则应使用.

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.

这篇关于Google Cloud授权的Python HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆