Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

working tutorial with code

ANSWERED

Anyone have a working tutorial of how to use the api to get users heart rate data?

Best Answer
0 Votes
2 BEST ANSWERS

Accepted Solutions

What is does the body of the request that returned a 401 say?

View best answer in original post

Best Answer
0 Votes

heartRate.jpgIts working now. Graph n all. Thanks. I just had to allow access to the app. 

View best answer in original post

Best Answer
0 Votes
9 REPLIES 9

Check out the Community Resources page in the docs.

Best Answer
0 Votes

Jeremiah,

 

 Hello from Ireland. Im trying to register my app. I'm using your javascript example from the community resources. Which url is the callback url? and how do I find mine? I really just need my own heart rate data for now. If the callback url consists of tokens and ids, how do I find this information.

 

Thanks,

Eoghan Hynes.

Best Answer
0 Votes

Ah, never mind, I seem to have firgured it out.

Best Answer
0 Votes

Hi,

 

 Masters student, thesis project. Trying to access my personal heart rate data...

 

My App:   https://dev.fitbit.com/apps/details/2282KN

 

Im trying to use your javascript example to get my personal heart rate data:

 

<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Fitbit Web API demo</title>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script>google.charts.load('current', {'packages':['line']});</script>
</head>
<body>
<p>hello world</p>
<div id="chart"></div>
<script>
console.log("begin");
// If user hasn't authed with Fitbit, redirect to Fitbit OAuth Implicit Grant Flow
var fitbitAccessToken;

if (!window.location.hash) {
window.location.replace('https://www.fitbit.com/oauth2/authorize?response_type=token&client_id=2282KN&redirect_uri=http%3A%2F...');
} else {
var fragmentQueryParameters = {};
window.location.hash.slice(1).replace(
new RegExp("([^?=&]+)(=([^&]*))?", "g"),
function($0, $1, $2, $3) { fragmentQueryParameters[$1] = $3; }
);

fitbitAccessToken = fragmentQueryParameters.access_token;
console.log("explicit token "+fitbitAccessToken);
}

// Make an API request and graph it
var processResponse = function(res) {
if (!res.ok) {
throw new Error('Fitbit API request failed: ' + res);
}

var contentType = res.headers.get('content-type')
if (contentType && contentType.indexOf("application/json") !== -1) {
return res.json();
console.log("data received");
} else {
throw new Error('JSON expected but received ' + contentType);
}
}

var processHeartRate = function(timeSeries) {
return timeSeries['activities-heart-intraday'].dataset.map(
function(measurement) {
return [
measurement.time.split(':').map(
function(timeSegment) {
return Number.parseInt(timeSegment);
}
),
measurement.value
];
}
);
}

var graphHeartRate = function(timeSeries) {
console.log(timeSeries);
var data = new google.visualization.DataTable();
data.addColumn('timeofday', 'Time of Day');
data.addColumn('number', 'Heart Rate');

data.addRows(timeSeries);

var options = google.charts.Line.convertOptions({
height: 450
});

var chart = new google.charts.Line(document.getElementById('chart'));

chart.draw(data, options);
}

fetch(
'https://api.fitbit.com/1/user/-/activities/heart/date/2016-10-01/1d/1sec/time/09:00/16:00.json',
//'https://api.fitbit.com/1/user/-/activities/heart.json',
{
headers: new Headers({
'Authorization': 'Bearer ' + fitbitAccessToken
}),
mode: 'cors',
method: 'GET'
}
).then(processResponse)
.then(processHeartRate)
.then(graphHeartRate)
.catch(function(error) {
console.log(error);
});
</script>
<!--<script src="./boot.js"></script>-->
</body>
</html>

 

 

Hosting on amazon: http://ec2-52-212-2-202.eu-west-1.compute.amazonaws.com/index.html

 

Getting error 401 in browser console log. Can you check if my app is set up ok and my urls are ok. Im not sure what im doing wrong. Do I have intra day access permission?

 

Would appreciate any help,

Thanks,

Eoghan Hynes. 

Best Answer
0 Votes

What is does the body of the request that returned a 401 say?

Best Answer
0 Votes

heartRate.jpgIts working now. Graph n all. Thanks. I just had to allow access to the app. 

Best Answer
0 Votes

Jeremiah,

 

 I can get the heart rate data fine using your javascript example but I would like to get other scopes now such as sleep and weight etc.

 

How do I do this in Javascript. I tried:

 

'https://api.fitbit.com/1/user/-/activities.json'

 

which in the console log gives:

 

data received:  function json() { [native code] }

 

&

 

 'https://api.fitbit.com/1/user/-/activities/sleep/date/2016-10-03/1d/1sec/time/12:00/01:00.json'

 

which gives:

 

GET https://api.fitbit.com/1/user/-/activities/sleep/date/2016-10-03/1d/1sec/time/12:00/01:00.json 400 ()
boot.js:96 Error: Fitbit API request failed: [object Response]
at processResponse2 (http://ec2-52-212-2-202.eu-west-1.compute.amazonaws.com/js/boot.js:71:15)

 

 What URLs can I use to get these scopes?

Best Answer
0 Votes

You'll need to request the correct time series endpoint. These are documented at https://dev.fitbit.com/docs/activity/#activity-time-series .

Best Answer
0 Votes

Excellent. Thank you. Now I can get all intra day data using the correct endpoints, eg:

 

var url = "https://api.fitbit.com/1/user/-/activities/calories/date/2016-10-08/1d/15min/time/09:30/19:00.json"

 

which is processed using a javascript function like :

 

var processCalories = function(timeSeries) {
return timeSeries['activities-calories-intraday'].dataset.map(
  function(measurement) {
      return [
                measurement.time.split(':').map(
                   function(timeSegment) {
                    return Number.parseInt(timeSegment);
                    }
                ),
            measurement.value 

      ];
   }
 );
}

 

But for non intraday values, like sleep logs:

 

GET https://api.fitbit.com/1/user/28H22H/sleep/date/2014-09-01.json

 

How do I parse or format this kind of response? It's obviously gonna be in a different format from intra day but I cant tell from the example in the community resources how to deal with it corretly. Could you give me an example?

 

I appreciate all of your help so far.

Thanks again.

Best Answer
0 Votes