Search code examples
javascriptnode.jsexpressinstagraminstagram-api

How to get instagram id once user has authenticated (instagram-node)


I'm trying to get the instagram id of a user once they have authenticated on my website. I have no problem authenticating since I can get an access_token when I log-in with my own account and I can use the following code assuming I know the instaid of the user

exports.renderUser = function(req, res){
 console.log("STARTING RENDERING");

 var userID = "280430135";

 api.user(userID, function(err, result, remaining, limit){
  if(err){
   console.log("current user " + err);
  }

  app.set('imgSource', result.profile_picture);
  app.use('/', routes);
  res.redirect('/');
 });
};

Am I supposed to get the instaid of the user from this authentication code?

exports.handleauth = function(req, res) {
  api.authorize_user(req.query.code, redirect_uri, function(err, result) {
    if (err) {
     console.log(err.body);
     res.send("Didn't work");
    } else {
     console.log('Yay! Access token is ' + result.access_token);
     res.redirect('/');
    }
  });
};

Any help would be greatly appreciated. I'm using instagram-node since I'm using express for my app.

Thanks!


Solution

  • Turns out a friend was able to answer my questions faster than I anticipated.

    exports.handleauth
    

    returns a json response with the user information in this format:

    {
     "access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
      "user": {
        "id": "1574083",
        "username": "snoopdogg",
        "full_name": "Snoop Dogg",
        "profile_picture": "..."
      }
    }
    

    source: https://instagram.com/developer/authentication/

    Therefore the instaid and other user information can be accessed by calling

    result.user.id
    result.user.full_name
    

    etc...