I am testing a Javascript/AJAX application that connects to FitBit API using Oauth 2.0 authentication to return heart rates, and print them on the console. The application was able to connect to Fitbit API and obtain an access token from teh Fitbit API. The problem is I am getting the error below every time I run the apps and it wont show the fitbit data on the console:
This site can’t be reached
localhost refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED**
I am running this application on Windows 10. My file structure is this:
C:\fitbit_javascript
Under the fitbit_javascript folder are two files: index.html amd app.js
Here are the codes:
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Fitbit API JavaScript</title>
</head>
<body>
<a href="https://www.fitbit.com/oauth2/authorize?response_type=token&client_id=22CQ9N&redirect_uri=http%3A%2F%2Flocalhost%2Ffitbit_javascript%2F&scope=activity%20heartrate%20location%20nutrition%20profile%20settings%20sleep%20social%20weight&expires_in=604800">Login to Fitbit</a>
<script src="app.js"></script>
</body>
</html>
app.js:
// get the url
var url = window.location.href;
//getting the access token from url
var access_token = url.split("#")[1].split("=")[1].split("&")[0];
// get the userid
var userId = url.split("#")[1].split("=")[2].split("&")[0];
console.log(access_token);
console.log(userId);
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.fitbit.com/1/user/'+ userId
+'/activities/heart/date/today/1w.json');
xhr.setRequestHeader("Authorization", 'Bearer ' + access_token);
xhr.onload = function() {
if (xhr.status === 200) {
console.log(xhr.responseText)
}
};
xhr.send();