I would like to use Google Fit REST API at the server and perform CRUD operation on Fit data. A user is authenticated, permissions for https://www.googleapis.com/auth/fitness.activity.write,https://www.googleapis.com/auth/fitness.body.read,https://www.googleapis.com/auth/fitness.body.write have been granted for fit data to the app. Once the user is authenticated the server receives UID and OAUTH_TOKEN.
UID = "XYZ"
OAUTH_TOKEN="FOO_BAR"
APP_SECRET_KEY = "XXX"
CLIENT_ID = "AAA"
But whenever I do get/post on rest API it throws 401 error.
import requests
url = "https://www.googleapis.com/fitness/v1/users/%s/dataSources"% UID
headers = {'content-type': 'application/json','Authorization': "Bearer %s" % token}
r = requests.get(url, headers=headers)
print r.status_code
print r.content
Output
401
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
This is supposed to list all available data sources as mentioned in https://developers.google.com/fit/rest/v1/get-started.
What's wrong here?
After spending many hours the solution to the problem is as follows.
UserId should be me
and access_token should be the latest one since access token gets expires in 60 minutes.
UID = "XYZ"
OAUTH_TOKEN="FOO_BAR"
APP_SECRET_KEY = "XXX"
CLIENT_ID = "AAA"
import requests
url = "https://www.googleapis.com/fitness/v1/users/me/dataSources"
headers = { 'content-type': 'application/json',
'Authorization': 'Bearer %s' % OAUTH_TOKEN }
r = requests.get(url, headers=headers)
print r.status_code
print r.content