import fitbit from fitbit.api import FitbitOauth2Client import fitbit.api import json from iniHandler import print_data, print_json, ReadCredentials, ReadTokens, WriteTokens from authHandler import GetNewAccessToken import datetime if __name__ == "__main__": ResourceTypes = ['steps', 'floors', 'caloriesOut'] #This is the Fitbit URL to use for the API call FitbitURL = "https://api.fitbit.com/1/user/-/profile.json" #Get credentials ClientID, ClientSecret = ReadCredentials() AccessToken, RefreshToken, ExpiresAt = ReadTokens() # GetNewAccessToken(RefreshToken) # Called when the OAuth token has been refreshed def token_updater(token): global AccessToken global RefreshToken global ExpiresAt AccessToken = token['access_token'] RefreshToken = token['refresh_token'] ExpiresAt = token['expires_at'] print_data(token, 0, 1) WriteTokens(AccessToken.encode('utf-8'), RefreshToken.encode('utf-8'), ExpiresAt) return token
import random from flask import Flask, request from threading import Thread import threading import time from multiprocessing import Process, Value, Manager import configparser, os from iniHandler import ReadCredentials, WriteTokens config = configparser.ConfigParser() ########################### # STEP 1: user confirmation ########################### CLIENT_KEY, CLIENT_SECRET = ReadCredentials() # host must be set explicitly in nokia developers partner app, otherwise rejected CALLBACK_URL = "http://127.0.0.1:8080/callback" AUTHORIZE_URL = 'https://account.health.nokia.com/oauth2_user/authorize2' ACCESS_TOKEN_URL = 'https://account.health.nokia.com/oauth2/token' API_RESOURCE_URL = 'https://api.health.nokia.com/v2/user?action=getdevice' auth_params = { "client_id": CLIENT_KEY, "state": str(random.getrandbits(64)), # to protect from CSRF "redirect_uri": CALLBACK_URL, "scope": "user.info,user.metrics,user.activity", # we want to get access to user info, user metrics and user activity i.e. everything "response_type": "code", }
#(http://pdwhomeautomation.blogspot.co.uk/2015/03/using-fitbit-api-on-raspberry-pi-with.html) import base64 import urllib #import urllib2 from urllib.error import URLError from urllib.parse import urlencode from urllib.request import Request, urlopen import json import os import sys import requests from iniHandler import print_json, ReadCredentials, WriteTokens #Get credentials from file client_id, client_secret = ReadCredentials() #URL to refresh the access token TokenURL = "https://api.fitbit.com/oauth2/token" #Some reponces defining API error handling responses Authorised = "Tokens valid" TokenRefreshedOK = "Token refreshed OK" Reauthorise = "Invalid token, reauthorise fitbit API" ErrorInAPI = "Error when making API call that I couldn't handle" #Make a HTTP POST to get new tokens def GetNewAccessToken(RefToken): print_json('status', 'Getting a new access token')
if cherrypy.engine.state == cherrypy.engine.states.STARTED: threading.Timer(1, cherrypy.engine.exit).start() if __name__ == '__main__': try: input = raw_input except NameError: pass if not (len(sys.argv) == 3): responce = input( "Get credentials from credentials.ini? (Y/N)\n").upper() if responce == "Y": id, secret = ReadCredentials() elif responce == "N": responce = input( "Would you like to enter them manually now? (Y/N)\n").upper() if responce == "Y": id = input("Enter client id:\n") secret = input("Enter client secret:\n") elif responce == "N": print( "Try again giving arguments: client id and client secret.") sys.exit(1) else: print("Invalid input.") sys.exit(1)