【问题标题】:Clean Instagram oauth using node.js and express and minimal middlewares使用 node.js 清理 Instagram oauth 和 express 和最小的中间件
【发布时间】:2014-07-26 20:14:11
【问题描述】:

我正在尝试获得一个干净的 Instagram oauth,而不依赖于诸如护照或 instagram-node 之类的中间件来学习该过程并拥有最大的控制权。我一直在尝试遵循 instagram 服务器端(显式)流程,这是一个两步操作:

  1. 请求访问代码
  2. 请求访问令牌

现在我的服务器设置使用:

express         = require('express'),
app             = express();

并开始我正在使用的第一步:

app.get('/', function(req, res){
  var url = 'https://api.instagram.com/oauth/authorize/?client_id='+CLIENT-ID+'&redirect_uri='+YOUR-REDIRECT-URI+'&response_type=code'
  res.redirect(url);
});

上述步骤将我正确发送到 instagram 进行身份验证,并且 instagram 的重定向回调在下面被拾取,此时 console.log 确实显示了正确的 instagram 代码。但是 res.set 部分是错误的,不起作用。

app.get('/auth/instagram/callback', function(req, res){

  console.log('/// here to keep track of how many times this is called');
  console.log('Instagram code: ', req.query.code);

  var url = 'https://api.instagram.com/oauth/access_token';

  res.set({
   'client_id' : 'CLIENT-ID',
   'client_secret' : 'CLIENT-SECRET',
   'grant_type' : 'authorization_code',
   'redirect_uri' : 'YOUR-REDIRECT-URI',
   'code' : req.query.code
  }).redirect(url);

});

不幸的是,它在这一点上挂起,显然没有返回正确的数据。

Instagram 建议执行以下操作,但我不确定这将如何快速翻译:

curl \-F 'client_id=CLIENT-ID' \
  -F 'client_secret=CLIENT-SECRET' \
  -F 'grant_type=authorization_code' \
  -F 'redirect_uri=YOUR-REDIRECT-URI' \
  -F 'code=CODE' \https://api.instagram.com/oauth/access_token

任何对此的见解都将受到欢迎!

感谢您的帮助。

【问题讨论】:

    标签: node.js curl express oauth-2.0 instagram


    【解决方案1】:

    这是对 Instagram 的 OAuth 第二部分的实际响应!可能不会

    var data = {'client_id' : process.env.FANCRAWLCLIENTID,
                 'client_secret' : process.env.FANCRAWLCLIENTSECRET,
                 'grant_type' : 'authorization_code',
                 'redirect_uri' : process.env.INSURIREDIRECT,
                 'code' : req.query.code
                };
    
    // Configure the request
    var options = {
        uri: 'https://api.instagram.com/oauth/access_token',
        method: 'POST',
        form: data
    }
    
    request(options, function (error, response, body) {
    
      // to convert the string body to a usable object
      var pbody = JSON.parse(body);
    
      // pbody should look like this:      
      // {"access_token":"8943851.83434d.697342341324jkfdjsf41afd784932a2e8",
      //   "user":
      //     {"username":"my_user_name",
      //     "bio":"blah blah...",
      //     "website":"http:\/\/www.something.com",
      //   "profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_851_73sq_115.jpg",
      //     "full_name":"Full Name",
      //     "id":"8943851"}
      //   }
    
    });
    

    享受!!!

    【讨论】:

      【解决方案2】:

      我建议学习护照代码(尤其是instagram)。

      无论如何,在获得code(这对您有效)后,您需要从您的后端代码向 Instagram 发送请求。所以你的代码看起来更像(我的头):

      app.get('/auth/instagram/callback', function(req, res){
      
        console.log('/// here to keep track of how many times this is called');
        console.log('Instagram code: ', req.query.code);
      
        var data = {
         'url': url
         'client_id' : 'CLIENT-ID',
         'client_secret' : 'CLIENT-SECRET',
         'grant_type' : 'authorization_code',
         'redirect_uri' : 'YOUR-REDIRECT-URI',
         'code' : req.query.code
        };
      
        var url = 'https://api.instagram.com/oauth/access_token';
      
        request.post({
        method: 'POST',
        url: url,
        body: JSON.stringify(data),
        },
        function (e, r, body) {
          //body will contain the access_token
         });
      });
      

      然后拿到token后就可以设置session等了

      【讨论】:

      • 谢谢 Eugenio,感谢您的宝贵时间和意见。不幸的是,我收到了第一个请求:request.post({ ^ SyntaxError: Unexpected token . at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) ) 在 Module.load (module.js:356:32) 在 Function.Module._load (module.js:312:12) 在 Module.require (module.js:364:17) 在 require (module.js:380 :17) at Object.<anonymous>
      • 是的,我是凭记忆写代码的。检查请求模块。当我恢复到可靠的连接时,我会修复它。
      • 如此接近!非常感谢您现在正在调查它现在回来:正文:{“code”:400,“error_type”:“OAuthException”,“error_message”:“您必须提供client_id”}
      • 非常感谢您在这方面的帮助。我尝试对客户端 ID 和密码以及访问代码进行硬编码,以确保它不是链接问题并且确实有效。我也试过:request.post(url).form(data);和同样的错误......
      • 你好 Eugenio:你有机会看看吗?到目前为止,我还没有成功。 :(
      【解决方案3】:

      好的,它可以为特定的 API 调用发布请求,但还没有 OAUTH 部分.. 和带有 instagram 安全标头。

      此示例是在您拥有用户的访问令牌时关注用户。

      var crypto      = require('crypto'),
          request     = require('request');
      
      var hmac = crypto.createHmac('SHA256', 'INSTAGRAM_CLIENT_ID');
          hmac.setEncoding('hex');
          hmac.write('IP_ADDRESS_127.0.0.1_OR_12.34.56.78');
          hmac.end();
      var hash = hmac.read();
      
      // Set the headers
      var headers = {
          'X-Insta-Forwarded-For': 'IP_ADDRESS_127.0.0.1_OR_12.34.56.78|'+hash
      }
      
      // Configure the request
      var options = {
          uri: 'https://api.instagram.com/v1/users/1234/relationship_ OR WHATEVER API CALL',
          qs: {'access_token': 'INSTAGRAM ACCESS TOKEN'},
          method: 'POST',
          headers: headers,
          form:{action:'follow'}
      }
      
      request(options, function (error, response, body) {
          // body response is what you are interested in
      
          // NOTE that the body info is a string response so use var your_variable = JSON.parse(body) to use it as an object.
      
          // Some exemples bellow
      
          // USER NOT EXISTANT
          // {"meta":{"error_type":"APINotFoundError","code":400,"error_message":"this user does not exist"}}
          //
          // successful response from unfollow
          // {"meta":{"code":200},"data":{"outgoing_status":"none","target_user_is_private":false}}
          //
          // NOT FOLLOWING OR FOLLOWED BY
          // {"meta":{"code":200},"data":{"outgoing_status":"none","target_user_is_private":false,"incoming_status":"none"}}
          //
          // you are following user 1234 but not followed back by them
          // {"meta":{"code":200},"data":{"outgoing_status":"follows","target_user_is_private":false,"incoming_status":"none"}}
          //
          // Following and followed by
          // {"meta":{"code":200},"data":{"outgoing_status":"follows","target_user_is_private":true,"incoming_status":"followed_by"}}
          //
          // PRIVATE users
          // {"meta":{"code":200},"data":{"outgoing_status":"requested","target_user_is_private":true}}
      
      });
      

      我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2020-05-14
        • 1970-01-01
        • 2012-10-20
        • 2011-07-14
        • 1970-01-01
        • 2012-03-13
        • 2014-07-21
        • 1970-01-01
        • 2017-07-25
        相关资源
        最近更新 更多