commit 6ae865cf83232de3c3ba1bbf22e88fa20b397e78 Author: jrtechs Date: Sat Apr 6 16:30:15 2019 -0400 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..aeafab5 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +```bash +npm install fs --save +npm install unirest --save +npm install express --save +npm install passport --save +npm install passport-fitbit-oauth2 --save +``` \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..2faf4ed --- /dev/null +++ b/config.json @@ -0,0 +1,6 @@ +{ + "port": 9000, + "clientID": "fit bit client id", + "clientSecret": "super secret", + "callbackURL": "https://jrtechs.student.rit.edu/auth/fitbit/callback" +} diff --git a/index.html b/index.html new file mode 100644 index 0000000..5116271 --- /dev/null +++ b/index.html @@ -0,0 +1,70 @@ + + + + + Title + + + + + + + + +
+
+ + +
+
+ + + + +
powered by Surfing Waves
+ +
+
+ + + + + \ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 0000000..e16e09a --- /dev/null +++ b/server.js @@ -0,0 +1,95 @@ +/** express app */ +const express = require("express"); + +/** Manages oauth 2.0 w/ fitbit */ +const passport = require('passport'); + +/** Used to make API calls */ +const unirest = require('unirest'); + +/** express app */ +const app = express(); + +/** pull config file */ +const utils = require("./utils.js"); +const config = utils.getFileAsJSON("config.json"); + + + + +app.use(passport.initialize()); +app.use(passport.session({ + resave: false, + saveUninitialized: true +})); + + +var FitbitStrategy = require( 'passport-fitbit-oauth2' ).FitbitOAuth2Strategy; + + +var accessTokenTemp = null; +passport.use(new FitbitStrategy({ + clientID: config.clientID, + clientSecret: config.clientSecret, + callbackURL: config.callbackURL + }, + function(accessToken, refreshToken, profile, done) + { + console.log(accessToken); + accessTokenTemp = accessToken; + done(null, { + accessToken: accessToken, + refreshToken: refreshToken, + profile: profile + }); + } +)); + +passport.serializeUser(function(user, done) { + done(null, user); +}); + +passport.deserializeUser(function(obj, done) { + done(null, obj); +}); + + +passport.authenticate('fitbit', { scope: ['activity','heartrate','location','profile'] }); + +app.get('/auth/fitbit', + passport.authenticate('fitbit', { scope: ['activity','heartrate','location','profile'] } +)); + +app.get( '/auth/fitbit/callback', passport.authenticate( 'fitbit', { + successRedirect: '/', + failureRedirect: '/error' +})); + + +app.get('/error', (request, result) => +{ + result.write("Error authenticating with Fitbit API"); + result.end(); +}); + + +app.get('/', (request, result) => +{ + if(accessTokenTemp == null) + { + result.redirect('/auth/fitbit'); + } + + unirest.get('https://api.fitbit.com/1/user/-/activities/steps/date/today/1m.json') + .headers({'Accept': 'application/json', 'Content-Type': 'application/json', Authorization: "Bearer " + accessTokenTemp}) + .end(function (response) + { + // result.write(response.body); + result.end(); + console.log(response.body); + }); +}); + +app.listen(PORT, () => + console.log(`App listening on port ${config.port}!`) +); \ No newline at end of file diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..b7b3533 --- /dev/null +++ b/utils.js @@ -0,0 +1,20 @@ + +const fs = require('fs'); + +module.exports= + { + /** + * + * @param fileName + * @returns {any} + */ + getFileAsJSON: function(fileName) + { + return JSON.parse(module.exports.getFile(fileName)); + }, + + getFile: function(filename) + { + return fs.readFileSync(filename, 'utf8'); + } + };