node.js - 如何在 NodeJS 中获取 Instagram 用户名和访问 token

标签 node.js express instagram passport.js

我正在使用 Instagram-passport 登录我的应用程序。用户登录后如何获取用户名和访问 token ?我无法访问中间件部分中的“配置文件”。

//serialize user in the session
passport.serializeUser(function (user, done) {
    done(null, user);
});
//deserialize user in the session
passport.deserializeUser(function (id, done) {
    User.findById(id, function (err, user) {
        done(err, user);
    });
});
//callback after successfull login of instagram
exports.callback = function (req, res) {
    // Successful authentication, redirect home.
    console.log('calling');
    var code = req.query.code;
    //console.log('accessToken:' + accessToken);
    console.log(req.session)
    getOption(code, function (option) {
        //console.log('option from getOption callback ' + util.inspect(option, false, null));
        request(option, function (error, response, body) {
            console.log('response inside request: ' + util.inspect(body, false, null));
            fs.writeFile("test", JSON.stringify(error), function (err) {
                if (err) {
                    console.log(err);
                } else {
                    console.log("The file was saved!");
                }
            });
            res.writeHead(200, {
                'Content-Type': 'text/json'
            });
            //res.end(response);


        });
    });
    // Successful authentication, redirect home
}
//function to get the option for the first parameter in the request function
function getOption(code, callback) {
    console.log('code from getOption ' + code)
    var options = {
        url: 'https://api.instagram.com/oauth/access_token',
        headers: {
            'client_id': 'client-id',
            'client_secret': 'client-secret`enter code here`',
            'grant_type': 'authorization_code',
            'redirect_uri': '/instagramcallback',
            'code': code
        }
    };
    callback(options);
}
//middleware 
passport.use(new InstagramStrategy({
    clientID: 'my client-id',
    clientSecret: 'my client secret',
    callbackURL: "/instagramcallback"
},
function (accessToken, refreshToken, profile, done) {
    // asynchronous verification, for effect...
         global.accessToken=accessToken;
        // To keep the example simple, the user's Instagram profile is returned to
        // represent the logged-in user. In a typical application, you would want
        // to associate the Instagram account with a user record in your database,
        // and return that user instead.
        console.log('client profile:'+profile);
        return done(null, profile);
}
));

最佳答案

var express = require('express');
var api = require('instagram-node').instagram();
var app = express();

app.configure(function() {
  // The usual...
});

api.use({
  client_id: YOUR_CLIENT_ID,
  client_secret: YOUR_CLIENT_SECRET
});

var redirect_uri = 'http://yoursite.com/handleauth';

exports.authorize_user = function(req, res) {
  res.redirect(api.get_authorization_url(redirect_uri, { scope: ['likes'], state: 'a state' }));
};

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.send('You made it!!');
    }
  });
};

// This is where you would initially send users to authorize
app.get('/authorize_user', exports.authorize_user);
// This is your redirect URI
app.get('/handleauth', exports.handleauth);

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

更多信息请引用链接:https://www.npmjs.com/package/instagram-node

关于node.js - 如何在 NodeJS 中获取 Instagram 用户名和访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24755748/

相关文章:

json - node.js 模块以可配置的方式比较 json

node.js - 如何使用 Node js从mongodb中的3个集合中获取数据?

javascript - 使用pdfkit生成pdf文件并发送到nodejs-expressjs中的浏览器

c# - WPF 在 Instagram 中查看图像标签

facebook-graph-api - Graph API中缺少页面的已连接Instagram帐户字段

python - 使用 selenium 和 webdriver (chrome) 在 Instagram 中填写登录表单 python OSX

node.js - 我应该保持数据库连接打开吗?

node.js - 启用 CORS 的服务器不拒绝请求

javascript - Express 4 + Angular 2 HTML 5 路由问题

javascript - 使用node.js和express.js的图片显示错误