python-3.x - 如何在 Google Cloud Functions 上部署基本的 Python 函数(带有包)
问题描述
我正在尝试部署一个基本的 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
我究竟做错了什么?该功能在我的本地机器上运行良好。
解决方案
有关详细信息,请参阅编写 HTTP 函数。这是我在查看您的代码时想到的:
- 缺少
request
参数 (def call_api(request):
) - 最后缺少返回(您不需要打印它,只需将其返回给调用者即可)
- 在文件末尾调用
call_api()
只会在本地调用函数,CF不需要这个
确保您正在部署使用gcloud functions deploy call_api --runtime python37 --trigger-http
推荐阅读
- c++ - 如何将中断向量表包装到 std::array 中?
- java - 如何在知道数组和数组长度的情况下计算数组的四分位距
- python - 将字符串分成 3 个一组的列表
- java - 如何使用 webflux 提供静态资源但支持 CACHE
- c++ - 从文件中逐字读取
- java - Cloud Pub/Sub Java 客户端 API 示例无法在本地运行
- java - 我正在尝试从每个循环的两个中检索结果
- javascript - 反应不重新获取过期的缓存文件
- android - Play Store 应用程序不会自动更新并出现错误:APP_IN_FOREGROUND
- unity3d - 使用 Physics 2D 拖动播放器对象