【问题标题】:Response status code 400 when using python module Requests with Sitescout API使用带有 Sitescout API 的 python 模块请求时的响应状态代码 400
【发布时间】:2015-10-31 12:06:27
【问题描述】:

我有一个客户端 ID 和客户端密码,并试图通过关注Sitescout's api docs 来生成一个身份验证令牌。我正在使用 python 模块请求并得到一个 400 状态代码,也就是格式错误的请求,但我似乎无法弄清楚为什么。

我的代码:

import requests

url = "https://api.sitescout.com/oauth/token"
headers = { 
            "Host": "api.sitescout.com",
            "Authorization": "Basic YmVldGhvdmVuOmxldG1laW4=",
            "Content-Type": "application/x-www-form-urlencoded",
            "Accept": "application/json",
            "Content-Length": "41"
        }

# Do the HTTP request
response = requests.post(url, headers=headers)

response.status_code

这是文档中提供的假 base64 授权标头,因此此 sn-p 返回 401 错误,即未经授权(我当然有自己的身份验证)。

谁能告诉我这是怎么回事?

【问题讨论】:

  • 你能把response.text()的输出贴出来吗?
  • response.text = {"error":"invalid_request"}

标签: python python-requests http-error


【解决方案1】:

已经解决了。我没有输入grant_type=client_credentials,添加后返回 200。

文档说“可选,您可以添加表单参数范围以指示您请求访问的范围;范围值必须用空格分隔(例如,STATS AUDIENCES)。”但它是可选的“&scope=STATS”参数,而不是所有参数。这个例子让我很困惑。

现在我的代码是

...

params = {  "grant_type" : "client_credentials" }

response = requests.post(url, headers=headers, params=params)

...

哪个有效。

【讨论】:

    猜你喜欢
    • 2020-01-14
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    相关资源
    最近更新 更多