III. Create a Streamr stream

Using the aforementioned access token, client ID and secret, you can get Google Fitness Data in the type of JSON object. To upload the data to the Streamr, you need to create a stream in the Streamr Network. The stream (topic) is an ordered sequence of events (timestamped data points). Event payloads are JSON objects, or in other words, a set of key-value pairs. You can find the tutorial how to create a stream here. After that, please write down the Stream ID and API Keys .

IV. Run the python script

Please copy the following code to your host, replace the $YOUR_LOGFILE_PATH , $YOUR_STREAM_ID , $YOUR_API_KEY , $YOUR_CLIENT_ID , $YOUR_CLIENT_SECRET , $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 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

token = '' # 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_ID = '$YOUR_CLIENT_ID'

CLIENT_SECRET = '$YOUR_CLIENT_SECRET'

REFRESH_TOKEN = '$YOUR_REFERSH_TOKEN'

""" Request Google Fitness API token using the refresh token

NOTE: Update every 45min

"""

global token

global CLIENT_ID

global CLIENT_SECRET

global REFRESH_TOKEN



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

url = '

body = ('{"client_secret":"%s", "grant_type":"refresh_token", "refresh_token":"%s", "client_id":"%s"}'%(CLIENT_SECRET, REFRESH_TOKEN, CLIENT_ID))

headers = '{"Content-Type":"application/x-www-form-urlencoded"}'

try:

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

token = req.json()['access_token']

LOG.info("Get user token successfully: %s", token)

return token

except:

import traceback

LOG.error("Unable to get user token")

traceback.print_exc()

return None def get_user_token():""" Request Google Fitness API token using the refresh tokenNOTE: Update every 45min"""global tokenglobal CLIENT_IDglobal CLIENT_SECRETglobal REFRESH_TOKENthd.Timer(2700,get_user_token).start()url = ' https://www.googleapis.com/oauth2/v4/token' body = ('{"client_secret":"%s", "grant_type":"refresh_token", "refresh_token":"%s", "client_id":"%s"}'%(CLIENT_SECRET, REFRESH_TOKEN, CLIENT_ID))headers = '{"Content-Type":"application/x-www-form-urlencoded"}'try:req = requests.post(url, params=headers, data=body)token = req.json()['access_token']LOG.info("Get user token successfully: %s", token)return tokenexcept:import tracebackLOG.error("Unable to get user token")traceback.print_exc()return None

""" Request User Daily Steps stored in Google Fitness Platform.

"""

# unix timestamp: xxx seconds. Note: Google fitness update steps data one day later.

start_time = time.time() - (time.time() % 86400) + time.timezone - 86400

end_time = time.time() - 86400

url = '

body = ('{"aggregateBy": [{"dataTypeName": "com.google.step_count.delta","dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"}],"bucketByTime": { "durationMillis": 86400000 },"startTimeMillis": %s, "endTimeMillis": %s}'%(start_time*1000, end_time*1000))

headers = {"Content-Type":"application/json","Authorization":"Bearer %s" % access_token}

try:

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

results_steps = req.json()

LOG.info("Get user daily steps successfully: %s", results_steps)

if len(results_steps['bucket'][0]['dataset'][0]['point']):

return results_steps['bucket'][0]['dataset'][0]['point'][0]['value'][0]['intVal']

else:

return None

except:

import traceback

LOG.error("Unable to get user daily steps.")

traceback.print_exc()

return None def get_user_steps(access_token):""" Request User Daily Steps stored in Google Fitness Platform."""# unix timestamp: xxx seconds. Note: Google fitness update steps data one day later.start_time = time.time() - (time.time() % 86400) + time.timezone - 86400end_time = time.time() - 86400url = ' https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate' body = ('{"aggregateBy": [{"dataTypeName": "com.google.step_count.delta","dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"}],"bucketByTime": { "durationMillis": 86400000 },"startTimeMillis": %s, "endTimeMillis": %s}'%(start_time*1000, end_time*1000))headers = {"Content-Type":"application/json","Authorization":"Bearer %s" % access_token}try:req = requests.post(url, headers=headers, data=body)results_steps = req.json()LOG.info("Get user daily steps successfully: %s", results_steps)if len(results_steps['bucket'][0]['dataset'][0]['point']):return results_steps['bucket'][0]['dataset'][0]['point'][0]['value'][0]['intVal']else:return Noneexcept:import tracebackLOG.error("Unable to get user daily steps.")traceback.print_exc()return None

""" Request User Daily Consumed Calories stored in Google Fitness Platform.

"""

# unix timestamp: xxx seconds. Note: Google fitness update steps data one day later.

start_time = time.time() - (time.time() % 86400) + time.timezone - 86400

end_time = time.time() - 86400

url = '

body = ('{"aggregateBy": [{"dataTypeName": "com.google.calories.expended","dataSourceId": "derived:com.google.calories.expended:com.google.android.gms:platform_calories_expended"}],"bucketByTime": { "durationMillis": 86400000 },"startTimeMillis": %s, "endTimeMillis": %s}'%(start_time*1000, end_time*1000))

headers = {"Content-Type":"application/json","Authorization":"Bearer %s" % access_token}

try:

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

results_calories = req.json()

LOG.info("Get user daily consumed calories successfully: %s", results_calories)

if len(results_calories['bucket'][0]['dataset'][0]['point']):

return results_calories['bucket'][0]['dataset'][0]['point'][0]['value'][0]['fpVal']

else:

return None

except:

import traceback

LOG.error("Unable to get user daily consumed calories.")

traceback.print_exc()

return None def get_user_calories(access_token):""" Request User Daily Consumed Calories stored in Google Fitness Platform."""# unix timestamp: xxx seconds. Note: Google fitness update steps data one day later.start_time = time.time() - (time.time() % 86400) + time.timezone - 86400end_time = time.time() - 86400url = ' https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate' body = ('{"aggregateBy": [{"dataTypeName": "com.google.calories.expended","dataSourceId": "derived:com.google.calories.expended:com.google.android.gms:platform_calories_expended"}],"bucketByTime": { "durationMillis": 86400000 },"startTimeMillis": %s, "endTimeMillis": %s}'%(start_time*1000, end_time*1000))headers = {"Content-Type":"application/json","Authorization":"Bearer %s" % access_token}try:req = requests.post(url, headers=headers, data=body)results_calories = req.json()LOG.info("Get user daily consumed calories successfully: %s", results_calories)if len(results_calories['bucket'][0]['dataset'][0]['point']):return results_calories['bucket'][0]['dataset'][0]['point'][0]['value'][0]['fpVal']else:return Noneexcept:import tracebackLOG.error("Unable to get user daily consumed calories.")traceback.print_exc()return None

""" Upload Google Fitness Data to Streamr

NOTE: Upload per minute

"""

global token

global STREAM_ID

global API_KEY

url = '

headers = {"Authorization":"token %s" % API_KEY}

steps = get_user_steps(token)

calories = get_user_calories(token)

if steps and calories:

body = ('{"Daily Steps": %s, "Daily Calories": %s}'%(steps,calories))

elif steps:

body = ('{"Daily Steps": %s, "Daily Calories": "Zero or not update"}'%steps)

elif calories:

body = ('{"Daily Steps": "Zero or not update", "Daily Calories": %s}'%calories)

else:

body = ('{"Daily Steps": "Zero or not update", "Daily Calories": "Zero or not update"}')

LOG.info("Google fitness data is: %s",body)

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

try:

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

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

LOG.info("Upload google fitness data to Streamr successfully.")

return req.status_code

else:

LOG.info("Fail to upload google fitness data to Streamr. ERROR: %s", req.status_code)

except:

import traceback

LOG.error("Fail to Upload Google Fitness Data to Streamr.")

traceback.print_exc()

return None def upload_to_streamr():""" Upload Google Fitness Data to StreamrNOTE: Upload per minute"""global tokenglobal STREAM_IDglobal API_KEYurl = ' https://www.streamr.com/api/v1/streams/'+STREAM_ID+'/data' headers = {"Authorization":"token %s" % API_KEY}steps = get_user_steps(token)calories = get_user_calories(token)if steps and calories:body = ('{"Daily Steps": %s, "Daily Calories": %s}'%(steps,calories))elif steps:body = ('{"Daily Steps": %s, "Daily Calories": "Zero or not update"}'%steps)elif calories:body = ('{"Daily Steps": "Zero or not update", "Daily Calories": %s}'%calories)else:body = ('{"Daily Steps": "Zero or not update", "Daily Calories": "Zero or not update"}')LOG.info("Google fitness data is: %s",body)thd.Timer(60, upload_to_streamr).start()try:req = requests.post(url, headers=headers, data=body)if req.status_code == 200 or req.status_code == 201:LOG.info("Upload google fitness data to Streamr successfully.")return req.status_codeelse:LOG.info("Fail to upload google fitness data to Streamr. ERROR: %s", req.status_code)except:import tracebackLOG.error("Fail to Upload Google Fitness 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 personal data in the stream!