Home > Mobile >  python requests not recognizing params
python requests not recognizing params

Time:12-31

I am requesting to mindbodyapi to get token with the following code using requests library

def get_staff_token(request):
    URL = "https://api.mindbodyonline.com/public/v6/usertoken/issue"
    payload = {
               'Api-Key': API_KEY,
               'SiteId': "1111111",
               'Username': '[email protected]',
               '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"))

CodePudding user response:

In the second one, you are passing the API Key in headers and the credentials in the body of the request. In the first, you are sending both the API Key and credentials together in the query string, not the request body. Refer to requests.request() docs

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': '[email protected]',
        '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')
  • Related