You need to use OAuth2 to get data with the Google Fit API. It is convenient to use google-api-python-client when retrieving data using OAuth2. The method to get the data using google-api-python-client is described below.
pip3 install google-api-python-client
getfit.py
import os
import json
import httplib2
import requests
import time
from datetime import datetime, timedelta
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow, flow_from_clientsecrets
from oauth2client.file import Storage
OAUTH_SCOPE = 'https://www.googleapis.com/auth/fitness.activity.read'
DATA_SOURCE = "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
CREDENTIALS_FILE = "./secret/credentials"
def auth_data():
credentials = ""
if os.path.exists(CREDENTIALS_FILE):
credentials = Storage(CREDENTIALS_FILE).get()
else:
#flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
flow = flow_from_clientsecrets(
#Specify the JSON file for OAuth acquired when API is enabled
'./secret/oauth2.json',
#Specify the scope
scope=OAUTH_SCOPE,
#Specify the token receiving method after user authentication (described later)
redirect_uri=REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print('Please start the following URL in your browser.')
print(authorize_url)
code = input('Please enter Code: ').strip()
credentials = flow.step2_exchange(code)
if not os.path.exists(CREDENTIALS_FILE):
Storage(CREDENTIALS_FILE).put(credentials)
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)
fitness_service = build('fitness', 'v1', http=http)
return fitness_service
def retrieve_data(fitness_service, dataset):
return fitness_service.users().dataSources(). \
datasets(). \
get(userId='me', dataSourceId=DATA_SOURCE, datasetId=dataset). \
execute()
def nanoseconds(nanotime):
"""
Convert to nanoseconds
"""
dt = datetime.fromtimestamp(nanotime // 1000000000)
return dt.strftime('%Y-%m-%d %H:%M:%S')
def logwrite(date, step):
with open('./data/step.log', 'a') as outfile:
outfile.write(str(date) + "," + str(step) + "\n")
if __name__ == "__main__":
authdata = auth_data()
#Get the data for the previous day
TODAY = datetime.today() - timedelta(days=1)
STARTDAY = datetime(TODAY.year, TODAY.month, TODAY.day, 0, 0, 0)
NEXTDAY = datetime(TODAY.year, TODAY.month, TODAY.day, 23, 59, 59)
NOW = datetime.today()
START = int(time.mktime(STARTDAY.timetuple())*1000000000)
NEXT = int(time.mktime(NEXTDAY.timetuple())*1000000000)
END = int(time.mktime(NOW.timetuple())*1000000000)
data_set = "%s-%s" % (START, NEXT)
while True:
if END < NEXT:
break
dataset = retrieve_data(authdata, data_set)
starts = []
ends = []
values = []
for point in dataset["point"]:
if int(point["startTimeNanos"]) > START:
starts.append(int(point["startTimeNanos"]))
ends.append(int(point["endTimeNanos"]))
values.append(point['value'][0]['intVal'])
print("From: {}".format(nanoseconds(min(starts))))
print("To: {}".format(nanoseconds(max(ends))))
print("Steps:{}".format(sum(values)))
step = sum(values)
startdate = STARTDAY.date()
logwrite(startdate, step)
STARTDAY = STARTDAY + timedelta(days=1)
NEXTDAY = NEXTDAY + timedelta(days=1)
START = int(time.mktime(STARTDAY.timetuple())*1000000000)
NEXT = int(time.mktime(NEXTDAY.timetuple())*1000000000)
data_set = "%s-%s" % (START, NEXT)
time.sleep(5)
-I tried to get Google Fit data in C #! -[Python] How to do Google API OAuth in embedded application
Recommended Posts