Calling External API with Requests keeps returning Error: could not handle the request

Calling External API with Requests keeps returning Error: could not handle the request

我正在尝试在 python 中编写一个 google 云函数,它只会从 API 中获取一些数据并显示它:

from flask import escape
import requests
import json

def randomFact(request):
    URL = "https://uselessfacts.jsph.pl/random.json?language=en"
    r = requests.get(URL).json()
    return r

这是requirement.txt:

# Function dependencies, for example:
# package>=version

functions-framework==3.0.0
flask==2.0.2
flask-restful==0.3.9
requests==2.11.0

然而,每次我 运行 它时,我只会收到一条回复:错误:无法处理请求。 我已经为该项目启用了计费,所以这不是问题,我已经在本地对其进行了测试并且它运行良好但是当我将它放入 google 云函数时它就不起作用了。

如果您知道解决此问题的任何方法或如何从 google 云函数调用外部 API,请提供帮助,我已经尝试了 3 天。

我能够重现您的问题。如果您查看云功能中的日志,它说存在 ssl 证书错误。

我尝试将您的 requirements.txt 更改为不包含 requests 上的版本号并且成功了:

functions-framework==3.0.0
requests
flask==2.0.2
flask-restful==0.3.9