使用nodejs获取instagram访问令牌

时间:2015-06-11 09:55:21

标签: javascript node.js instagram instagram-api

我尝试使用Instagram节点模块在不使用浏览器请求(通过http.get()请求)的情况下获取访问令牌,但我无法接收任何访问令牌,请在没有浏览器交互的情况下为我提供解决方案。

var express = require("express");
var app = express();
var http = require('http')
var api = require('instagram-node').instagram();
 app.configure(function () {
 app.use(express.cookieParser());
 app.use(express.session({ secret: 'secret' }));
 app.use(express.bodyParser());
 app.use(express.json());
 app.use(express.urlencoded());
});
api.use({
   client_id: '',
   client_secret: ''
 });
 var redirect_uri = 'http://localhost:5000/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('', 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!!');
    }
   });
  };
 app.get('/authorize_user', exports.authorize_user);
 app.get('/handleauth', exports.handleauth);
 http.createServer(app).listen(5000, function(){
 console.log("Express server listening on port 5000");
     **http.get('http://localhost:5000/authorize_user', function(res) {
      console.log("statusCode: ", res.statusCode);
      });**
  });

1 个答案:

答案 0 :(得分:0)

如果没有浏览器请求,您将无法获取访问令牌,因为它必须提示用户登录并接受权限。将“http://localhost:5000/authorize_user”重定向到instagram.com,然后如果单击“确定”,则返回到您的网站。