We are in the process of migrating this forum. A new space will be available soon. We are sorry for the inconvenience.

Fail to fetch token with python requests (curl is ok)


Arn-O
08-07-2014, 12:15 PM
Finally found the answer by myself:

Code:
r = s.post(TOKEN_URL, data=payload, headers=headers)
"data" should be used and not "params".

Arn-O
08-07-2014, 11:30 AM
Hoho:

https://github.com/kennethreitz/requests/issues/223

Arn-O
08-07-2014, 11:27 AM
Hi.

I follow the instruction from the sandbox and the docs:

https://api.hubic.com/docs/
https://api.hubic.com/sandbox/

The step when I fecth the token fails. Here is the snippet:

Code:
credential64 = base64.b64encode(client_id + ":" + secret_key)
autho = 'Basic ' + credential64
payload = {
            'code': code,
            'redirect_uri': urllib.quote_plus(REDIRECT_URI),
            'grant_type': 'authorization_code'
        }
headers = {
            'Authorization': autho,
            'Content-Type': 'application/x-www-form-urlencoded'
        }
r = s.post(TOKEN_URL, params=payload, headers=headers)
I get an error 400:

Code:
{u'error_description': u'missing arguments', u'error': u'invalid_request'}
The URL looks ok:

Code:
https://api.hubic.com/oauth/token/?grant_type=authorization_code&code=14073944452KpBvKQCD9sLnsfSqJ1fr1dFRQMvYlsHvCixJDr8Ha772LR0DZOCaWzxORBXoRdx&redirect_uri=http%253A%252F%252Flocalhost%253A8080%252Foauth%252F
The headers look ok either:

Code:
CaseInsensitiveDict({'Content-Length': '0', 'Accept-Encoding': 'gzip, deflate, compress', 'Accept': '*/*', 'User-Agent': 'python-requests/2.2.1 CPython/2.7.6 Linux/3.13.0-32-generic', 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic YXBpX2h1YmljX1RDcGdhWUg3b0JVaWc0ZHNGYm1jM3J5T2hIRDZJdnhZOjloWkJoMDMyY21FaGxwQk5UUnQySTBNb0txZGlmaGJETVVkUjh0Sk9Gb0NZWnZidklmRkYwWFh2eTZ0MGVDcGI='})
The worst is that it works with curl:

Code:
curl 'https://api.hubic.com/oauth/token/' -H 'Authorization: Basic YXBpX2h1YmljX1RDcGdhWUg3b0JVaWc0ZHNGYm1jM3J5T2hIRDZJdnhZOjloWkJoMDMyY21FaGxwQk5UUnQySTBNb0txZGlmaGJETVVkUjh0Sk9Gb0NZWnZidklmRkYwWFh2eTZ0MGVDcGI=' --data 'code=1407343969mAIKBD5pyPSjDDI37NCmLorVAcNwJ2NRMbJuTJYV6Ea7d2IiMo6xk7a3qBRsVsxB&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Foauth%2F&grant_type=authorization_code'
The headers used by curl may help:

Code:
> POST /oauth/token/ HTTP/1.1
> User-Agent: curl/7.35.0
> Host: api.hubic.com
> Accept: */*
> Authorization: Basic YXBpX2h1YmljX1RDcGdhWUg3b0JVaWc0ZHNGYm1jM3J5T2hIRDZJdnhZOjloWkJoMDMyY21FaGxwQk5UUnQySTBNb0txZGlmaGJETVVkUjh0Sk9Gb0NZWnZidklmRkYwWFh2eTZ0MGVDcGI=
> Content-Length: 163
> Content-Type: application/x-www-form-urlencoded
>
Any idea?

Thanks!