【问题标题】:why post request has different result?为什么发布请求有不同的结果?
【发布时间】:2016-07-06 06:59:12
【问题描述】:

我在 linux 中使用 curl 将数据发布到 asana 并且工作正常。

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

但如果我使用 Python 的 requests 库,结果是 400

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

【问题讨论】:

    标签: python curl python-requests asana-api


    【解决方案1】:

    您需要在 python 中为您的请求添加相同的“Authorization: Bearer”令牌标头。类似于这个答案:

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

    【讨论】:

      【解决方案2】:

      requests 方法的 auth 参数生成 Authorization: Basic 标头,而不是 Authorization: Bearer 标头。

      使用手动创建的 Authorization 标头设置 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'})
      

      【讨论】:

        猜你喜欢
        • 2023-01-17
        • 2017-02-16
        • 1970-01-01
        • 1970-01-01
        • 2019-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多