Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Python access token

ANSWERED

I'm having trouble using python to get an access token. I keep getting back Error 400.

import requests
import base64

base_url = "https://api.fitbit.com/oauth2/token/"

toConvert = "####:################################"
encoded_data = base64.b64encode(toConvert)

autho = "Basic" + " " + str(encoded_data) + "="
#print autho

#From wordpress site
authorization_code = "789ce4a20fdf01d78b010a9c7133e555e908cac7"

access_token_req = {
    "client_id" : "######",
    "grant_type" : "authorization_code",
    "redirect_uri" : "http://diegowp.duckdns.org/wordpress/",
    "code" : authorization_code,
}

#Form request
req = requests.Request('POST', base_url + autho, data = access_token_req)
prepared = req.prepare()

#Send and print response
s = requests.Session()
response = s.send(prepared)
print "Response from fitbit: " + str(response)

I changed the https to http and captured the request with Wireshark.

POST /oauth2/token/Basic%20#######################################= HTTP/1.1
Host: api.fitbit.com
Accept-Encoding: identity
Content-Length: 153
Content-Type: application/x-www-form-urlencoded

code=789ce4a20fdf01d78b010a9c7133e555e908cac7&grant_type=authorization_code&client_id=######&redirect_uri=http%3A%2F%2Fdiegowp.duckdns.org%2Fwordpress%2F

Any ideas? Thanks.

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Doh. You're right. Here's what changed. Maybe somebody else will use it later on.

 

header = {"Authorization" : autho}
r = requests.post(base_url, headers = header, data = access_token_req, stream = True)

 I was just able to get back sleep data. Love it. 

View best answer in original post

Best Answer
6 REPLIES 6

What exactly does response says? It should say why you're getting 400.

Also changing to http may not help since you will be getting different exception message saying that you're using non secure connection.

Ivan Bahdanau
Senior Software Developer at Fitbit
Best Answer
0 Votes

Attached is the error message:

{"errors":[{"errorType":"oauth","fieldName":"n/a","message":"invalid_request, Missing grant_type parameter value"}],"success":false}

Looks like the same problem on this other thread (last post): https://community.fitbit.com/t5/Web-API/OAuth2-0-access-Token-API-not-working-properly/m-p/816862#U8...

Best Answer
0 Votes

I got it working. I didn't change anything in code. I did refresh my "code" after each time it didn't work. I also just gave it time. Maybe your server was mad at me for so many bad attempts.

Best Answer
0 Votes

It'd take a lot of bad requests for our servers to get mad on the oauth2/* endpoints 🙂

Best Answer
0 Votes

@imagiro1 wrote:
I changed the https to http and captured the request with Wireshark.
POST /oauth2/token/Basic%20#######################################= HTTP/1.1
Host: api.fitbit.com
Accept-Encoding: identity
Content-Length: 153
Content-Type: application/x-www-form-urlencoded

code=789ce4a20fdf01d78b010a9c7133e555e908cac7&grant_type=authorization_code&client_id=######&redirect_uri=http%3A%2F%2Fdiegowp.duckdns.org%2Fwordpress%2F

The Authorization header seems to be colliding with the POST /oauth2/token line. Not sure if this is actually the request being sent or a copy/paste problem.

Best Answer
0 Votes

Doh. You're right. Here's what changed. Maybe somebody else will use it later on.

 

header = {"Authorization" : autho}
r = requests.post(base_url, headers = header, data = access_token_req, stream = True)

 I was just able to get back sleep data. Love it. 

Best Answer