forked from jplattel/fitbit.py
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfitbit.py
84 lines (71 loc) · 3.92 KB
/
fitbit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
A Python library for accessing the FitBit API.
This library provides a wrapper to the FitBit API and does not provide storage of tokens or caching if that is required.
Most of the code has been adapted from: https://groups.google.com/group/fitbit-api/browse_thread/thread/0a45d0ebed3ebccb
5/22/2012 - JCF - Updated to work with python-oauth2 https://github.com/dgouldin/python-oauth2
"""
import os, httplib
import oauth2 as oauth
# pass oauth request to server (use httplib.connection passed in as param)
# return response as a string
class FitBit():
CONSUMER_KEY = 'ADD_YOUR_CONSUMER_KEY'
CONSUMER_SECRET = 'ADD_YOUR_CONSUMER_SECRET'
SERVER = 'api.fitbit.com'
REQUEST_TOKEN_URL = 'http://%s/oauth/request_token' % SERVER
ACCESS_TOKEN_URL = 'http://%s/oauth/access_token' % SERVER
AUTHORIZATION_URL = 'http://%s/oauth/authorize' % SERVER
DEBUG = False
def FetchResponse(self, oauth_request, connection, debug=DEBUG):
url = oauth_request.to_url()
connection.request(oauth_request.method,url)
response = connection.getresponse()
s=response.read()
if debug:
print 'requested URL: %s' % url
print 'server response: %s' % s
return s
def GetRequestToken(self):
connection = httplib.HTTPSConnection(self.SERVER)
consumer = oauth.Consumer(self.CONSUMER_KEY, self.CONSUMER_SECRET)
signature_method = oauth.SignatureMethod_PLAINTEXT()
oauth_request = oauth.Request.from_consumer_and_token(consumer, http_url=self.REQUEST_TOKEN_URL)
oauth_request.sign_request(signature_method, consumer, None)
resp = self.FetchResponse(oauth_request, connection)
auth_token = oauth.Token.from_string(resp)
#build the URL
authkey = str(auth_token.key)
authsecret = str(auth_token.secret)
auth_url = "%s?oauth_token=%s" % (self.AUTHORIZATION_URL, auth_token.key)
return auth_url, auth_token
def GetAccessToken(self, access_code, auth_token):
oauth_verifier = access_code
connection = httplib.HTTPSConnection(self.SERVER)
consumer = oauth.Consumer(self.CONSUMER_KEY, self.CONSUMER_SECRET)
signature_method = oauth.SignatureMethod_PLAINTEXT()
oauth_request = oauth.Request.from_consumer_and_token(consumer, token=auth_token, http_url=self.ACCESS_TOKEN_URL, parameters={'oauth_verifier': oauth_verifier})
oauth_request.sign_request(signature_method, consumer, auth_token)
# now the token we get back is an access token
# parse the response into an OAuthToken object
access_token = oauth.Token.from_string(self.FetchResponse(oauth_request,connection))
# store the access token when returning it
access_token = access_token.to_string()
return access_token
def ApiCall(self, access_token, apiCall='/1/user/-/activities/log/steps/date/today/7d.json'):
#other API Calls possible, or read the FitBit documentation for the full list.
#apiCall = '/1/user/-/devices.json'
#apiCall = '/1/user/-/profile.json'
#apiCall = '/1/user/-/activities/date/2011-06-17.json'
signature_method = oauth.SignatureMethod_PLAINTEXT()
connection = httplib.HTTPSConnection(self.SERVER)
#build the access token from a string
access_token = oauth.Token.from_string(access_token)
consumer = oauth.Consumer(self.CONSUMER_KEY, self.CONSUMER_SECRET)
final_url = 'http://' + self.SERVER + apiCall
oauth_request = oauth.Request.from_consumer_and_token(consumer, token=access_token, http_url=final_url)
oauth_request.sign_request(signature_method, consumer, access_token)
headers = oauth_request.to_header(realm='api.fitbit.com')
connection.request('GET', apiCall, headers=headers)
resp = connection.getresponse()
json = resp.read()
return json