Created
March 24, 2014 15:54
-
-
Save ntrepid8/9743064 to your computer and use it in GitHub Desktop.
FitBit OAuth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
import random | |
from hashlib import sha1 | |
import hmac | |
import binascii | |
from getpass import getpass | |
from urllib import urlencode, quote, quote_plus | |
from urlparse import parse_qs | |
from pprint import pprint, pformat | |
import mechanize | |
fitbit_user_email = raw_input('email: ') | |
fitbit_user_password = getpass() | |
request_url = 'https://api.fitbit.com/oauth/request_token' | |
authorize_url = 'https://www.fitbit.com/oauth/authorize' | |
access_url = 'https://api.fitbit.com/oauth/access_token' | |
callback_url = 'https://maasive.net' | |
consumer_key = raw_input('consumer_key: ') | |
consumer_secret = raw_input('consumer_secret: ') | |
# OAuth 1.0 Leg One | |
oa_random = random.randrange(1000000000) | |
ts = int(time.time()) | |
osm = 'HMAC-SHA1' | |
params = [ | |
('oauth_callback', callback_url), | |
('oauth_consumer_key', consumer_key), | |
('oauth_nonce', oa_random), | |
('oauth_signature_method', osm), | |
('oauth_timestamp', ts), | |
('oauth_version', '1.0'), | |
] | |
sig_base = '&'.join([ | |
'GET', | |
quote_plus(request_url), | |
quote(urlencode(params)) | |
]) | |
print('signature_base: %s' % sig_base) | |
key = quote(consumer_secret)+'&' | |
hashed = hmac.new(key.encode(), sig_base.encode(), sha1) | |
oauth_signature = quote_plus(binascii.b2a_base64(hashed.digest())[:-1].decode()) | |
params.insert(2, ('oauth_signature', oauth_signature)) | |
print('signature: %s' % oauth_signature) | |
auth_header = 'OAuth '+', '.join(['%s=\"%s\"' % (i[0], i[1]) for i in params]) | |
print('auth_header: %s' % auth_header) | |
r = requests.get(request_url, headers={'Authorization': auth_header}) | |
print('GET %s %d' % (r.request.url, r.status_code)) | |
leg_one_response = parse_qs(r.text) | |
print('response: %s' % pformat(leg_one_response)) | |
request_token = leg_one_response['oauth_token'][-1] | |
request_token_secret = leg_one_response['oauth_token_secret'][-1] | |
redirect_url = '?'.join([ | |
authorize_url, | |
'oauth_token='+request_token | |
]) | |
print('authorize_url: %s' % redirect_url) | |
# OAuth 1.0 Leg Two | |
br = mechanize.Browser() | |
br.open(redirect_url) | |
br.select_form(name="oauth") | |
br['email'] = fitbit_user_email | |
br['password'] = fitbit_user_password | |
leg_two_response = br.submit() | |
pprint(leg_two_response.geturl()) | |
# pprint(leg_two_response.info().items()) | |
# pprint(leg_two_response.read()) | |
oauth_verifier = leg_two_response.info().get('oauth_verifier') | |
authorization_token = leg_two_response.info().get('oauth_token') | |
print('oauth_verifier: %s' % oauth_verifier) | |
# OAuth 1.0 Leg Three | |
oa_random = random.randrange(1000000000) | |
ts = int(time.time()) | |
osm = 'HMAC-SHA1' | |
params = [ | |
('oauth_consumer_key', consumer_key), | |
('oauth_nonce', oa_random), | |
('oauth_signature_method', osm), | |
('oauth_timestamp', ts), | |
('oauth_token', request_token), | |
('oauth_verifier', oauth_verifier), | |
('oauth_version', '1.0'), | |
] | |
sig_base = '&'.join([ | |
'POST', | |
quote_plus(access_url), | |
quote(urlencode(params)) | |
]) | |
key = quote(consumer_secret)+'&'+quote(request_token_secret) | |
hashed = hmac.new(key.encode(), sig_base.encode(), sha1) | |
oauth_signature = quote_plus(binascii.b2a_base64(hashed.digest())[:-1].decode()) | |
params.insert(1, ('oauth_signature', oauth_signature)) | |
print('signature: %s' % oauth_signature) | |
auth_header = 'OAuth '+', '.join(['%s=\"%s\"' % (i[0], i[1]) for i in params]) | |
print('auth_header: %s' % auth_header) | |
r = requests.post(access_url, headers={'Authorization': auth_header}) | |
print('POST %s %d' % (r.request.url, r.status_code)) | |
access_response = parse_qs(r.text) | |
print('access_response: %s' % access_response) | |
errors = access_response.get('errors', []) | |
if errors: | |
print('errors: %s' % pformat(errors)) | |
else: | |
access_token = access_response.get('oauth_token', [])[-1] | |
access_token_secret = access_response.get('oauth_token_secret', [])[-1] | |
access_user_id = access_response.get('encoded_user_id', [])[-1] | |
print('access_token: %s' % access_token) | |
print('access_token_secret: %s' % access_token_secret) | |
print('access_user_id: %s' % access_user_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment