Next you need to copy and paste everything in the address bar but without the starting part ( https://www.streamr.com/ ) to the Parse response section, and hit enter key once. This way you can clearly see what the token is, what the scope is, and how long the token is valid.

Step 3: Make request and get the data!

After you are done with the Parse response , the next step is ready for you. You need to create a Streamr stream at first. Then please copy the following code to your host, replace the $YOUR_LOGFILE_PATH , $YOUR_STREAM_ID , $YOUR_API_KEY , $YOUR_CLIENT_BASH64 , $YOUR_ACCESS_TOKEN , $YOUR_REFERSH_TOKEN with yours, and keep running the script for live data to appear in a stream. Note that, this particular script has been coded in python 2.7. If you use python 3.x or other script languages, you will need to re-code it first.

# -*- coding: utf-8 -*- import requests

import json

import logging as LOG

import hashlib

import time

import threading as thd LOG.basicConfig(level=LOG.DEBUG,

format=’%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s’,

datefmt=’%a, %d %b %Y %H:%M:%S’,

filename=’YOUR_LOGFILE_PATH’,

filemode=’a’) # global variable # Get the following infomation from the Streamr

STREAM_ID = ‘$YOUR_STREAM_ID’

API_KEY = ‘$YOUR_API_KEY’ # Get the following information from the Google Fitness Platform

CLIENT_BASH64 = ‘$YOUR_CLIENT_BASH64’

ACCESS_TOKEN = ‘$YOUR_ACCESS_TOKEN’

REFRESH_TOKEN = ‘$YOUR_REFERSH_TOKEN’

“”” Request Fitbit Access token using the refresh token

NOTE: Update every 45min

“””

global ACCESS_TOKEN

global REFRESH_TOKEN

global CLIENT_BASH64



thd.Timer(2700,get_user_token).start()

url = ‘

body = “grant_type=refresh_token&refresh_token=%s”%(REFRESH_TOKEN)

paras = {‘Authorization’: “Basic %s”%(CLIENT_BASH64), ‘Content-Type’: “application/x-www-form-urlencoded”}

try:

req = requests.post(url, headers=paras, data=body)

ACCESS_TOKEN = req.json()[‘access_token’]

REFRESH_TOKEN = req.json()[‘refresh_token’]

LOG.info(“Get user token successfully: Access_Token: %s Refresh_Token: %s”, ACCESS_TOKEN, REFRESH_TOKEN)

return ACCESS_TOKEN

except:

import traceback

LOG.error(“Unable to get user token”)

traceback.print_exc()

return None def get_user_token():“”” Request Fitbit Access token using the refresh tokenNOTE: Update every 45min“””global ACCESS_TOKENglobal REFRESH_TOKENglobal CLIENT_BASH64thd.Timer(2700,get_user_token).start()url = ‘ https://api.fitbit.com/oauth2/token’ body = “grant_type=refresh_token&refresh_token=%s”%(REFRESH_TOKEN)paras = {‘Authorization’: “Basic %s”%(CLIENT_BASH64), ‘Content-Type’: “application/x-www-form-urlencoded”}try:req = requests.post(url, headers=paras, data=body)ACCESS_TOKEN = req.json()[‘access_token’]REFRESH_TOKEN = req.json()[‘refresh_token’]LOG.info(“Get user token successfully: Access_Token: %s Refresh_Token: %s”, ACCESS_TOKEN, REFRESH_TOKEN)return ACCESS_TOKENexcept:import tracebackLOG.error(“Unable to get user token”)traceback.print_exc()return None

“”” The Get Device endpoint returns a list of the Fitbit devices connected to a user’s account.

“””

url = ‘

paras = {‘Authorization’: “Bearer %s”%(access_token)}

try:

req = requests.get(url, headers=paras)

devices_info = json.dumps(req.json())

LOG.info(“Get user devices info successfully: %s”, devices_info)

return devices_info

except:

import traceback

LOG.error(“Unable to get devices info”)

traceback.print_exc()

return None def get_user_devices(access_token):“”” The Get Device endpoint returns a list of the Fitbit devices connected to a user’s account.“””url = ‘ https://api.fitbit.com/1/user/-/devices.json' paras = {‘Authorization’: “Bearer %s”%(access_token)}try:req = requests.get(url, headers=paras)devices_info = json.dumps(req.json())LOG.info(“Get user devices info successfully: %s”, devices_info)return devices_infoexcept:import tracebackLOG.error(“Unable to get devices info”)traceback.print_exc()return None def get_user_activities(access_token):

“”” The Get Daily Activity Summary endpoint retrieves a summary and list of a user’s activities and activity log entries for a given day in the format requested using units in the unit system which corresponds to the Accept-Language header provided.

“””

# Current date time in the format “yyyy-MM-dd”.

date = time.strftime(“%F”)

url = ‘https://api.fitbit.com/1/user/-/activities/date/%s.json' % date

paras = {“Authorization”:”Bearer %s” % access_token}

try:

req = requests.get(url, headers=paras)

activities_info = json.dumps(req.json())

LOG.info(“Get user activities informantion successfully: %s”, activities_info)

return activities_info

except:

import traceback

LOG.error(“Unable to get user activities information.”)

traceback.print_exc()

return None

“”” The Get Heart Rate Time Series endpoint returns time series data in the specified range for a given resource in the format requested using units in the unit systems that corresponds to the Accept-Language header provided.

“””

url = ‘

paras = {“Authorization”:”Bearer %s” % access_token}

try:

req = requests.get(url, headers=paras)

heartrate_info = json.dumps(req.json())

LOG.info(“Get user heartrate information successfully: %s”, heartrate_info)

return heartrate_info

except:

import traceback

LOG.error(“Unable to get user heartrate information.”)

traceback.print_exc()

return None def get_user_heartrate(access_token):“”” The Get Heart Rate Time Series endpoint returns time series data in the specified range for a given resource in the format requested using units in the unit systems that corresponds to the Accept-Language header provided.“””url = ‘ https://api.fitbit.com/1/user/-/activities/heart/date/today/1d.json' paras = {“Authorization”:”Bearer %s” % access_token}try:req = requests.get(url, headers=paras)heartrate_info = json.dumps(req.json())LOG.info(“Get user heartrate information successfully: %s”, heartrate_info)return heartrate_infoexcept:import tracebackLOG.error(“Unable to get user heartrate information.”)traceback.print_exc()return None

“”” Upload the Fitbit Data to Streamr

NOTE: Upload per minute

“””

global ACCESS_TOKEN

global STREAM_ID

global API_KEY

url = ‘

paras = {“Authorization”:”token %s” % API_KEY}

result_devices = get_user_devices(ACCESS_TOKEN)

if not result_devices:

result_devices = “the Data not update”

result_activities = get_user_activities(ACCESS_TOKEN)

if not result_activities:

result_activities = “the Data not update”

result_heartrate = get_user_heartrate(ACCESS_TOKEN)

if not result_heartrate:

result_heartrate = “the Data not update”

body = (‘{“User Devices”: %s, “Daily Activities”: %s, “Daily HeartRate”: %s}’%(result_devices,result_activities,result_heartrate))

LOG.info(“The Fitbit data is: %s”,body)

thd.Timer(500, upload_to_streamr).start()

try:

req = requests.post(url, headers=paras, data=body)

if req.status_code == 200 or req.status_code == 201:

LOG.info(“Upload the Fitbit data to Streamr successfully.”)

return req.status_code

else:

LOG.info(“Fail to upload the Fitbit data to Streamr. ERROR: %s”, req.status_code)

except:

import traceback

LOG.error(“Fail to Upload the Fitbit data to Streamr.”)

traceback.print_exc()

return None def upload_to_streamr():“”” Upload the Fitbit Data to StreamrNOTE: Upload per minute“””global ACCESS_TOKENglobal STREAM_IDglobal API_KEYurl = ‘ https://www.streamr.com/api/v1/streams/'+STREAM_ID+'/data' paras = {“Authorization”:”token %s” % API_KEY}result_devices = get_user_devices(ACCESS_TOKEN)if not result_devices:result_devices = “the Data not update”result_activities = get_user_activities(ACCESS_TOKEN)if not result_activities:result_activities = “the Data not update”result_heartrate = get_user_heartrate(ACCESS_TOKEN)if not result_heartrate:result_heartrate = “the Data not update”body = (‘{“User Devices”: %s, “Daily Activities”: %s, “Daily HeartRate”: %s}’%(result_devices,result_activities,result_heartrate))LOG.info(“The Fitbit data is: %s”,body)thd.Timer(500, upload_to_streamr).start()try:req = requests.post(url, headers=paras, data=body)if req.status_code == 200 or req.status_code == 201:LOG.info(“Upload the Fitbit data to Streamr successfully.”)return req.status_codeelse:LOG.info(“Fail to upload the Fitbit data to Streamr. ERROR: %s”, req.status_code)except:import tracebackLOG.error(“Fail to Upload the Fitbit data to Streamr.”)traceback.print_exc()return None if __name__==”__main__”:

get_user_token()

upload_to_streamr()

Unless an error has occurred, you should be able to see your Fitbit fitness data in the stream!