From ad5b50770217afa497dd138d6f189ba7f34f0dda Mon Sep 17 00:00:00 2001 From: John Tantalo Date: Sat, 20 Dec 2014 13:18:17 -0800 Subject: [PATCH] added setup script and instructions for fitbit credentials --- README.md | 12 +++++++- auth_fitbit.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100755 auth_fitbit.py diff --git a/README.md b/README.md index bcf5ad0..c9278cd 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,23 @@ Use `pip` to install the required packages, $ pip install -r requirements.txt +### Fitbit Credentials + +[Register a Fitbit application](https://dev.fitbit.com/apps/new). Note the client key and secret. + +Run `auth_fitbit.py` to get credentials for read access to a user's data, + + $ python auth_fitbit.py [FITBIT CLIENT KEY] [FITBIT CLIENT SECRET] + +This scripts open a browser window where you can log in to your Fitbit account and authorize the app to "access your profile and data". When you accept, the site will give you a string to copy and paste back into the script, which then writes the credentials to a local file named `fitbit.yaml`. + ### Google Credentials [Create a project in Google Developers Console and enable the fitness API](https://console.developers.google.com/flows/enableapi?apiid=fitness). Note the client id and client secret. Run `auth_google.py` to get credentials for write access to a user's body data, - $ python auth_google.py [YOUR CLIENT ID] [YOUR CLIENT SECRET] https://www.googleapis.com/auth/fitness.body.write + $ python auth_google.py [GOOGLE CLIENT ID] [GOOGLE CLIENT SECRET] https://www.googleapis.com/auth/fitness.body.write This script opens a browser window where you can log in to your Google account and authorize the app to "View and store body sensor data in Google Fit". When you accept, the script writes the credentials to a local file named `google.json`. diff --git a/auth_fitbit.py b/auth_fitbit.py new file mode 100755 index 0000000..a1a591e --- /dev/null +++ b/auth_fitbit.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +""" +This was taken, and modified from python-fitbit/gather_keys_cli.py, +License reproduced below. + +-------------------------- +The MIT License + +Copyright (c) 2007 Leah Culver + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +Example consumer. This is not recommended for production. +Instead, you'll want to create your own subclass of OAuthClient +or find one that works with your web framework. +""" + +import os +import sys +import webbrowser +import yaml + +from fitbit.api import FitbitOauthClient + + +def gather_keys(): + # setup + client = FitbitOauthClient(CLIENT_KEY, CLIENT_SECRET) + + # get request token + token = client.fetch_request_token() + + stderr = os.dup(2) + os.close(2) + os.open(os.devnull, os.O_RDWR) + webbrowser.open(client.authorize_token_url()) + os.dup2(stderr, 2) + try: + verifier = raw_input('Verifier: ') + except NameError: + # Python 3.x + verifier = input('Verifier: ') + + # get access token + token = client.fetch_access_token(verifier) + return token + + +if __name__ == '__main__': + if not (len(sys.argv) == 3): + print("Arguments 'client key', 'client secret' are required") + sys.exit(1) + CLIENT_KEY = sys.argv[1] + CLIENT_SECRET = sys.argv[2] + + keys = gather_keys() + credentials = dict( + client_key=CLIENT_KEY, + client_secret=CLIENT_SECRET, + resource_owner_key=keys['oauth_token'].encode('ascii'), + resource_owner_secret=keys['oauth_token_secret'].encode('ascii')) + yaml.dump(credentials, open('fitbit.yaml', 'w'))