如何通过身份验证从 Google Apps 脚本调用云函数? [英] How to call a Cloud Function from Google Apps Script with Authentication?
问题描述
我正在尝试使用Hello World"云函数代码示例进行测试,但是当我调用它时,即使我已经将 Invoker 角色添加到请求者用户电子邮件中,我也会收到 401 HTTP 错误.对于运行此功能的请求,我还需要做些什么吗?
I'm trying to do a test with the "Hello World" Cloud Function code sample, but when I call it I get 401 HTTP error even I've already added the Invoker role to the requester user email. Is there anything else that I need to do on the request to run this function?
我正在尝试在 Google Scripts 中执行这样的 GET:
I'm trying to do the GET like this in Google Scripts:
function test(){
var url = 'https:******************/HelloWorld';
var response = UrlFetchApp.fetch(url);
Logger.log(response.getContentText())
}
PythonHello World"云函数代码示例是:
And the Python "Hello World" Cloud Function code sample is:
def hello_world(request):
"""Responds to any HTTP request.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text or any set of values that can be turned into a
Response object using
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
"""
request_json = request.get_json()
if request.args and 'message' in request.args:
return request.args.get('message')
elif request_json and 'message' in request_json:
return request_json['message']
else:
return f'Hello World!'
推荐答案
能否成功浏览 Cloud Functions 端点?
Can you browse the Cloud Functions endpoint successfully?
https:******************/HelloWorld
在我的例子中(你可以试试这个),我的 HelloWorld 示例的 URL 使用了默认的 function-1
名称,所以我的 URL 是:
In my case (you may try this), my URL for the HelloWorld example used the default function-1
name, so my URL is:
https://us-central1-dazwilkin-200204-60062279.cloudfunctions.net/function-1
我使用上面的 URL 创建了一个 Apps 脚本函数,它可以工作:
I created an Apps Script function using the above URL and it works:
[20-02-04 10:44:18:857 PST] Hello World!
这篇关于如何通过身份验证从 Google Apps 脚本调用云函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!