python-3.x - 如何在 Google Cloud Functions 上部署基本的 python 函数(带包)

标签 python-3.x http google-cloud-platform google-cloud-functions

我正在尝试部署一个基本的 python 函数,该函数通过 Google Functions(浏览器编辑器)使用 HTTP 触发器调用 api。

这是我要部署的功能:

import requests
import json

def call_api():

    API_URL = 'https://some-api.com/v1'
    API_TOKEN = 'some-api-token'

    result = requests.get(API_URL+"/contacts?access_token="+API_TOKEN).json()
    print(result)

call_api()

我的 requirements.txt 包含:

requests==2.21.0

但是,每次我尝试部署该函数时,都会出现以下错误:

Unknown resource type

我做错了什么?该功能在我的本地机器上运行良好。

最佳答案

请引用Writing HTTP Functions了解更多信息。这是我在查看您的代码时想到的:

  • 缺少 request 参数 (def call_api(request):)
  • 最后缺少return(不用打印,直接返回给调用者即可)
  • 在文件末尾调用call_api()只会在本地调用函数,CF不需要这个

确保您正在使用 gcloud functions deploy call_api --runtime python37 --trigger-http

进行部署

关于python-3.x - 如何在 Google Cloud Functions 上部署基本的 python 函数(带包),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55999927/

相关文章:

python - 将 Pandas 系列(基于索引)切片为数据框的多列

http - 未指定此类 header 时,浏览器如何知道内容类型?

c++ - google-cloud-cpp CMake 生成失败

machine-learning - Google Cloud AutoML 中的身份验证问题

java - 即使启用版本控制,Google 云存储 Blob.delete() 也不会创建存档

python - 调用 PutObject 操作时的 PermanentRedirect

python - 如何将日志记录模块中的单个记录器列入白名单

python - 在Python中访问类中的类成员的最佳方法

javascript - 使用 JavaScript 强制执行 http

AngularJS 检查几个 $http get 请求的结果