为什么 post 请求有不同的结果?

why post request has different result?

我在 linux 中使用 curl 到 post 数据到 asana 并且它的工作正常。

curl -H "Authorization: Bearer <mytoken>" https://app.asana.com/api/1.0/tasks/101/stories -d "text=hello world"

但是如果我使用 python 的请求库,结果是 400

response = requests.post("https://app.asana.com/api/1.0/tasks/101/stories", auth=(self.asana.token_uri, ""), params = "text=repo_name")

您需要向 python 中的请求添加相同的 "Authorization: Bearer" 令牌 header。类似于这个答案:

https://whosebug.com/a/29931730/6080374

requests 方法的 auth 参数产生 Authorization: Basic header,而不是 Authorization: Bearer header.

使用手动创建的 Authorization header 设置 headers 词典。 POST 数据(curl 的 -d 命令行开关)应作为字典传递给 data 参数:

response = requests.post(
    "https://app.asana.com/api/1.0/tasks/101/stories",
    headers={'Authorization': 'Bearer {}'.format(self.asana.token_uri),
    data={'text': 'repo_name'})