Я тестирую приложение Javascript / AJAX, которое подключается к API FitBit, используя проверку подлинности Oauth 2.0, чтобы вернуть частоту сердечных сокращений и распечатать их на консоли.Приложение смогло подключиться к Fitbit API и получить токен доступа из Fitbit API.Проблема в том, что я получаю сообщение об ошибке ниже каждый раз, когда запускаю приложения, и оно не отображает данные fitbit на консоли:
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();