05-04-2016 04:32
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-04-2016 04:32
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi,
I'm developing an app to get fitbit information.
And i have a problem. That is why i'm here. I want first step login on my website with local login or social networks. And then with fitbit.
i show you the error:
GET /auth/fitbit/callback?code= - - ms - - InternalOAuthError: Failed to obtain access token (status: 400 data: {"errors":[{"errorType":"invalid_grant","message":"Authorization code invalid: Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false}) at Strategy.OAuth2Strategy._createOAuthError (/home/usuario/easy-node-authentication-master/node_modules/passport-oauth2/lib/strategy.js:370:17) at /home/usuario/easy-node-authentication-master/node_modules/passport-oauth2/lib/strategy.js:166:45 at /home/usuario/easy-node-authentication-master/node_modules/oauth/lib/oauth2.js:177:18 at passBackControl (/home/usuario/easy-node-authentication-master/node_modules/oauth/lib/oauth2.js:123:9) at IncomingMessage.<anonymous> (/home/usuario/easy-node-authentication-master/node_modules/oauth/lib/oauth2.js:143:7) at IncomingMessage.emit (events.js:117:20) at _stream_readable.js:944:16 at process._tickCallback (node.js:458:13) GET /auth/fitbit/callback?code= 500 501.697 ms - 1088
server.js
passport.use("fitbit",new FitbitStrategy({ clientID: config.fitbitClientId, clientSecret: config.fitbitClientSecret, scope: config.fitbitScope, callbackURL: config.fitbitCallbackURL }, function(req, token, tokenSecret, profile, done) { // asynchronous process.nextTick(function() { // check if the user is already logged in if (!req.user) { UserFit.findOne({ fitbitId: profile.id }, function(err, user) { if (err) return done(err); if (user) { // if there is a user id already but no token (user was linked at one point and then removed) if (!profile.refreshToken) { profile.refreshToken = refreshToken; user.save(function(err) { if (err) return done(err); return done(null, user); }); } return done(null, user); // user found, return that user } else { // if there is no user, create them user = UserFit.fromFitbitProfile(profile); console.log('Creating user account for ' + profile.id); //modificado update por save UserFit.update(function(err) { if (err) return done(err); return done(null, UserFit); }); } }); } }); }));
UserFit.js
var mongoose = require("mongoose"); var UserFit = mongoose.model("UserFit", { refreshToken: String, aboutMe: String, age: Number, avatar: String, aveargeDailySteps: Number, dateOfBirth: Date, displayName: String, fullName: String, distanceUnit: String, fitbitId: String, gender: String, glucoseUnit: String, height: Number, heightUnit: String, locale: String, weight: Number, weightUnit: String, timezone: String, country: String }); UserFit.fromFitbitProfile = function(profile) { var data = profile._json.user; console.log(profile); console.log(data); return new UserFit({ refreshToken: profile.refreshToken, aboutMe: data.aboutMe, age: data.age, avatar: data.avatar, aveargeDailySteps: data.aveargeDailySteps, dateOfBirth: data.dateOfBirth, displayName: profile.displayName, fullName: data.fullName, distanceUnit: data.distanceUnit, fitbitId: profile.id, gender: data.gender, glucoseUnit: data.glucoseUnit, height: data.height, heightUnit: data.heightUnit, locale: data.locale, weight: data.weight, weightUnit: data.weightUnit, timezone: data.timezone, country: data.country }); }; module.exports = UserFit;

- Labels:
-
JavaScript
05-04-2016 13:00
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



05-04-2016 13:00
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi @JavierJavier, we can't debug your code, but we're happy to debug your HTTP requests and responses. Please try to capture the requests and share here (remember to obfuscate your Auth header). You can use a tool like Runscope Traffice Inspector to do this.
Given the error 'data' you posted, it looks like your app is not passing the 'code' value correctly.

