Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InternalOAuthError: Failed to obtain access token passport-instagram

I'm having a trouble with passport-instagram, I'm trying to implement passport-instagram to my application. Whenever I try to login using instagram login button I will get this error: InternalOAuthError: Failed to obtain access token {no matching code}

Here's the code

instagram.js (passport instagram configuration file)

var passport = require('passport');
var InstagramStrategy = require('passport-instagram').Strategy;
var User = require('../models/user');

var instaOpt = {
  clientID: "aad229e3597643be92568acb46efb40d",
  clientSecret: "5563070a33394e0fafada92a16ec2e71",
  callbackURL: "http://localhost:8000/auth/instagram/callback"
};

var instagramInit = function(accessToken, refreshToken, profile, callback) {
  User.findOne({ "instagram.id" : profile.id } , function(err, user) {
    if (err) return callback(err);

    if (user) {
      return callback(null, user); // User already exist
    }

    var newUser = new User();
    newUser.instagram.id = profile.id;
    newUser.instagram.token = accessToken;
    newUser.instagram.email = profile.email;
    newUser.instagram.displayName = profile.displayName;
    newUser.instagram.name = profile.name;
    newUser.instagram.username = profile.username;
    // newUser.instagram.picture = profile.picture;

    newUser.save(function(err) {
      if (err) {
        throw err;
      }
      return callback(null, newUser);
    });
  });
}

passport.use(new InstagramStrategy(instaOpt, instagramInit));

passport.serializeUser(function(user, done) {
  done(null, user);
});

passport.deserializeUser(function(obj, done) {
  done(null, obj);
});


module.exports = {
  instagramLogin: passport.authenticate("instagram"),
  instagramCallback: passport.authenticate("instagram", {
    successRedirect: "/profile",
    failureRedirect: "/"
  })
}

router.js

var router = require('express').Router();
var authConfig = require('../config/auth-config');
var auth = require('../config/instagram');

router.get('/', function(req, res) {
  res.render('index.ejs');
});

router.get('/logout', function(req, res) {
  req.logout();
  res.redirect('/');
});


router.get('/profile', function(req, res) {
  res.render('profile.ejs', {
    user: req.user
  });
});

router.get('/auth/instagram', auth.instagramLogin);
router.get('/auth/instagram/callback', auth.instagramCallback);

module.exports = router;

database.js (the User Schema file)

var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var Schema = mongoose.Schema;

var User = new Schema({
  instagram: {
    id: String,
    token: String,
    email: String,
    displayName: String,
    username: String,
    name: String,
    picture: String,
  }
});

module.exports = mongoose.model('User', User);

Instagram client (Will change the client and secret later) enter image description here

Now I'm really confuse, where did i do wrong? maybe it is something obvious.

like image 827
Jack Moscovi Avatar asked Aug 28 '15 09:08

Jack Moscovi


1 Answers

Use {failWithError: true}. For example:

passport.authenticate('oath2', {failWithError: true})
like image 172
Rajat Chaudhary Avatar answered Sep 21 '22 01:09

Rajat Chaudhary