#!/usr/bin/env python """ This was taken, and modified from python-fitbit/gather_keys_oauth2.py, License reproduced below. -------------------------- Copyright 2012-2015 ORCAS Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ import os import sys import threading import traceback import webbrowser import yaml from base64 import b64encode import cherrypy from fitbit.api import FitbitOauth2Client from oauthlib.oauth2.rfc6749.errors import MismatchingStateError, MissingTokenError from requests_oauthlib import OAuth2Session class OAuth2Server: def __init__(self, client_id, client_secret, redirect_uri='http://localhost:8080/'): """ Initialize the FitbitOauth2Client """ self.redirect_uri = redirect_uri self.success_html = """
%s' % ('\n'.join(tb)) if tb else '' return self.failure_html % (message, tb_html) def _shutdown_cherrypy(self): """ Shutdown cherrypy in one second, if it's running """ if cherrypy.engine.state == cherrypy.engine.states.STARTED: threading.Timer(1, cherrypy.engine.exit).start() def main(): if not (len(sys.argv) == 3): print("Arguments 'client ID', 'client secret' are required") sys.exit(1) client_id = sys.argv[1] client_secret = sys.argv[2] server = OAuth2Server(client_id, client_secret) server.browser_authorize() credentials = dict( client_id=client_id, client_secret=client_secret, access_token=server.oauth.token['access_token'], refresh_token=server.oauth.token['refresh_token']) yaml.dump(credentials, open('fitbit.yaml', 'w')) if __name__ == '__main__': main()