【问题标题】:How to call a Cloud Function from Google Apps Script with Authentication?如何通过身份验证从 Google Apps 脚本调用云函数?
【发布时间】:2020-05-20 13:40:31
【问题描述】:

我正在尝试使用“Hello World”云函数代码示例进行测试,但是当我调用它时,即使我已经将 Invoker 角色添加到请求者用户电子邮件中,我也会收到 401 HTTP 错误。对于运行此功能的请求,我还需要做些什么吗?

我正在尝试在 Google Scripts 中执行这样的 GET:

function test(){
   var url = 'https:******************/HelloWorld';
   var response = UrlFetchApp.fetch(url);
   Logger.log(response.getContentText())
}

Python“Hello World”云函数代码示例是:

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!'

【问题讨论】:

    标签: python-3.x google-apps-script google-cloud-functions


    【解决方案1】:

    能否成功浏览 Cloud Functions 端点?

    https:******************/HelloWorld

    在我的例子中(你可以试试这个),我的 HelloWorld 示例的 URL 使用了默认的 function-1 名称,所以我的 URL 是:

    https://us-central1-dazwilkin-200204-60062279.cloudfunctions.net/function-1

    我使用上面的 URL 创建了一个 Apps 脚本函数,它可以工作:

    [20-02-04 10:44:18:857 PST] Hello World!
    

    【讨论】:

    • 嗨@DazWilkin,我尝试使用函数名称(HelloWorldfunction-1)进行浏览,但出现 403 HTTP 错误!您是否进行了其他配置来构建您的功能?
    • Cloud Functions 是可以公开访问的,因此您应该也可以在您的 Apps 脚本代码中使用我的 URL 进行测试。我没有做任何额外的配置,而是从编辑器中运行了 Apps Script 函数。
    • 如果您安装了gcloud,您可以使用:gcloud functions describe function-1 --project=${PROJECT} --format="value(httpsTrigger.url)" 确认端点。对于function-1(如果存在),这将返回其 HTTPS 端点
    • 列出函数gcloud functions list --project=${PROJECT].
    • 对不起——再多说一句——如果你不能浏览云功能,那是你的问题。
    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 2016-12-21
    • 2015-12-07
    • 2022-06-13
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多