How can I read data from Google Fit into a variable with Tasker?
More speficially, I want to read e.g. my current step count for today which is counted by my phone + a fitness tracker and given to Google Fit. My goal is to have a variable containing this number to then work with it. I've read about the Google Fit History API but I don't know how to access it via Tasker.
I did figure it out after a day of trial and error. It's a rather "complicated" procedure.
Preparation:
Step 1 - Create a project on https://console.developers.google.com/project and get a client ID as well as the secret. (There's plenty of more detailed tutorials on that). The project needs to be configured as an app - not as a web thingy. You also need to add urn:ietf:wg:oauth:2.0:oob
to the redirection URLs. (http://localhost
may stay there)
Step 2 - Install RESTask Plugin for Tasker.
Step 3 - Goto https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/fitness.activity.read&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=YOUR_CLIENT_ID
. You will get a code there. Copy that code.
Step 4 - Create a Task which will use the RESTask plugin action. Configure it to:
Request Type: POST
Host: https://accounts.google.com/o/oauth2/token
Check Enable custom body
Set the custom body to: code=%THE_COPIED_CODE&client_id=%YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code
Switch to Header
and press Add more
:
Name: Content-Type
Value: application/x-www-form-urlencoded
You'll get a return code 200 if everything went fine.
Split the returned response (%rtres
) with splitter
"
. Save your token which lies within %rtres4
and your refresh token which lies within %rtres14
.
Getting Step Count
Step 1 - Before getting anything, you need to have a valid token. The token obtained during the preparation is only valid for a limited amount of time. You can request a new token with the refresh token that you should have saved. To get a working token, redo Step 4 with a different body:
client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&refresh_token=YOUR_REFRESH_TOKEN&grant_type=refresh_token
Repeat the splitting and the fresh token will be saved in %rtres4
.
Step 2 - Now you can finally get the precious data. At first, define a start and end time (saved in %start
and %end
). Note that those have to be in nanoseconds. You may use %TIMEMS*1000000
. Therefore, add a RESTask action with the following configuration:
Request Type: GET
Host: https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:com.google.android.gms:estimated_steps/datasets/%start-%end
Do not check Enable custom body
Switch to Header
and press Add more
(3 times):
Name: Content-Type
Value: application/json
Name: Authorization
Value: Bearer YOUR_TOKEN
Name: X-JavaScript-User-Agent
Value: Google APIs Explorer
You'll get a return code 200 if everything went fine. The step counts will be returned in the response.
Feel free to do whatever you want with this data. To get the complete step count, you may need to summarize the values. (Split the response and iterate with a for loop). I did chose to pass the step count to a Zooper Widget to have my own custom Google Fit step count widget on the homescreen.