Here is the code:
<!DOCTYPE html>
<html>
<head>
<title>Google Fitness API</title>
<meta charset='utf-8' />
</head>
<body>
<p>Get your step counts using the Google Fitness API.</p>
<!--Add buttons to initiate auth sequence and sign out-->
<button id="authorize-button" style="display: none;">Authorize</button>
<button id="signout-button" style="display: none;">Sign Out</button>
<div id="content"></div>
<script type="text/javascript">
// Enter an API key from the Google API Console:
// https://console.developers.google.com/apis/credentials?project=_
var apiKey = 'API_key';
// Enter a client ID for a web application from the Google API Console:
var clientId = 'XYZ';
// Enter one or more authorization scopes. Refer to the documentation for
// the API or https://developers.google.com/identity/protocols/googlescopes
// for details.
var scopes = 'https://www.googleapis.com/auth/fitness.activity.read';
var auth2; // The Sign-In object.
var authorizeButton = document.getElementById('authorize-button');
var signoutButton = document.getElementById('signout-button');
function handleClientLoad() {
// Load the API client and auth library
gapi.load('client:auth2', initAuth);
}
function initAuth() {
gapi.client.setApiKey(apiKey);
gapi.auth2.init({
client_id: clientId,
scope: scopes
}).then(function () {
auth2 = gapi.auth2.getAuthInstance();
// Listen for sign-in state changes.
auth2.isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(auth2.isSignedIn.get());
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
});
}
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.display = 'none';
signoutButton.style.display = 'block';
makeApiCall();
} else {
authorizeButton.style.display = 'block';
signoutButton.style.display = 'none';
}
}
function handleAuthClick(event) {
auth2.signIn();
}
function handleSignoutClick(event) {
auth2.signOut();
}
// Load the API and make an API call.
function makeApiCall() {
gapi.client.load('fitness', 'v1', function() {
var request = gapi.client.fitness.users.dataSources.datasets.get({
userId: 'me',
dataSourceId: 'com.google.step_count.delta',
datasetId: '1476092378000000-' + new Date().getTime() + '000000',
});
request.execute(function(resp) {
console.log(resp);
});
});
console.log(auth2.currentUser.get().getBasicProfile().getGivenName());
}
</script>
<script src="https://apis.google.com/js/api.js?onload=handleClientLoad"></script>
</body>
</html>
And here is what I get from the console:
Object {minStartTimeNs: "1476092378000000", maxEndTimeNs: "1476461775789000000", dataSourceId: "com.google.step_count.delta", point: Array[0], result: Object} dataSourceId: "com.google.step_count.delta" maxEndTimeNs: "1476461775789000000" minStartTimeNs: "1476092378000000" point: Array[0]
Other than that I don't get error messages in the console. Am I not supposed to get a series of values? If not, how could I do it? I must admit I'm fairly new to APIs :) Many thanks for your help!