简体   繁体   English

python 请求无法识别参数

[英]python requests not recognizing params

I am requesting to mindbodyapi to get token with the following code using requests library我正在请求 mindbodyapi 使用 requests 库使用以下代码获取令牌

def get_staff_token(request):
    URL = "https://api.mindbodyonline.com/public/v6/usertoken/issue"
    payload = {
               'Api-Key': API_KEY,
               'SiteId': "1111111",
               'Username': 'user@xyz.com',
               'Password': 'xxxxxxxx',
               }
    r = requests.post(url=URL, params=payload)
    print(r.text)
    return HttpResponse('Done')

gives a response as follows给出如下回应

{"Error":{"Message":"Missing API key","Code":"DeniedAccess"}}

But if I request the following way it works, anybody could tell me, what I am doing wrong on the above code.但是,如果我要求按照以下方式工作,任何人都可以告诉我,我在上面的代码中做错了什么。

conn = http.client.HTTPSConnection("api.mindbodyonline.com")
                payload = "{\r\n\t\"Username\": \"username\",\r\n\t\"Password\": \"xxxxx\"\r\n}"
                headers = {
                    'Content-Type': "application/json",
                    'Api-Key': API_KEY,
                    'SiteId': site_id,
                    }
                conn.request("POST", "/public/v6/usertoken/issue", payload, headers)
                res = conn.getresponse()
                data = res.read()
                print(data.decode("utf-8"))

In the second one, you are passing the API Key in headers and the credentials in the body of the request.在第二个中,您在标头中传递 API 密钥,并在请求正文中传递凭据。 In the first, you are sending both the API Key and credentials together in the query string, not the request body.首先,您将 API 密钥和凭据一起发送到查询字符串中,而不是请求正文中。 Refer to requests.request() docs请参阅requests.request()文档

Just use two dictionaries like in your second code and the correct keywords, I think it should work:只需使用第二个代码中的两个字典和正确的关键字,我认为它应该可以工作:

def get_staff_token(request):
    URL = "https://api.mindbodyonline.com/public/v6/usertoken/issue"
    payload = {
        'Username': 'user@xyz.com',
        'Password': 'xxxxxxxx',
    }
    headers = {
        'Content-Type': "application/json",
        'Api-Key': API_KEY,
        'SiteId': "1111111",
    }
    r = requests.post(url=URL, data=payload, headers=headers)
    print(r.text)
    return HttpResponse('Done')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM