06-04-2015 08:56 - edited 06-04-2015 08:57
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-04-2015 08:56 - edited 06-04-2015 08:57
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.
Answered! Go to the Best Answer.
Accepted Solutions
06-05-2015 11:21
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-05-2015 11:21
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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.
06-04-2015 09:05
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


06-04-2015 09:05
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.
Senior Software Developer at Fitbit

06-05-2015 07:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-05-2015 07:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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...

06-05-2015 09:05
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-05-2015 09:05
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.

06-05-2015 11:01
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



06-05-2015 11:01
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
It'd take a lot of bad requests for our servers to get mad on the oauth2/* endpoints 🙂

06-05-2015 11:06
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



06-05-2015 11:06
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
@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.

06-05-2015 11:21
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-05-2015 11:21
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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.
