2017-06-21 163 views
0

不工作这是我的HTTP POST代码,在本地主机上运行:Instagram的API通过的NodeJS

if(headers['Content-Type'] == undefined) 
     headers['Content-Type'] = 'application/x-www-form-urlencoded'; 

    var post_options = { 
      host: host, 
      path: path, 
      port: port, 
      method: 'POST', 
      headers: headers 
    }; 

    if(headers['Content-Type'] == "application/json"){ 
     post_options["json"] = true; 
     var post_data = JSON.stringify(body); 
    }else 
     var post_data = querystring.stringify(body); 

    var post_req = http.request(post_options, function(res) { 

      var body = ''; 

      console.log("INSIDE CALLBACK HTTP POST"); 

      res.setEncoding('utf8'); 

      res.on('data', function (chunk) { 
       body += chunk; 
       console.log('Response: ' + chunk); 
      }); 

      res.on('end', function() { 
      var post = querystring.parse(body); 
      console.log("FINAL BODY:",post); 
      }); 

      //console.log("RESPONSE in http POST:",res); 

     }); 

     // post the data 
     console.log("WRITING HTTP POST DATA"); 
     var sent_handler = post_req.write(post_data); 

     console.log("POST_REQ:",post_req); 
     console.log("sent_handler:",sent_handler); 

     post_req.end(); 

这里是我发送到Instagram的准确信息:

  • host =“api.instagram。 COM”
  • path = “/ OAuth的/的access_token”
  • body如下:

    body [“client_id”] = CLIENT_ID;

    body [“client_secret”] = CLIENT_SECRET;

    body [“grant_type”] =“authorization_code”;

    body [“redirect_uri”] = AUTHORIZATION_REDIRECT_URI;

    body [“code”] = login_code;

    body [“scope”] =“public_content”;

  • headers = {}(空,假定报头[ '内容 - 类型'] ==未定义为真)

  • 重要:sent_handler返回false

  • 的的console.log为“FINAL BODY”(变量post)返回“{}”

要注意:通讯使用curl与api instagram作品。所以我真的相信这个问题出现在nodejs代码的某些部分。

有没有人有任何想法?请询问是否需要更多信息

+0

请不要混淆,并从你的if语句它只是要求匹配省略括号发生灾难,并大大降低了可读性。 – brandonscript

+0

@brandonscript关于请求可能发生什么的任何想法? – Fane

回答

0

好的,所以有三个主要问题会导致失败,我可以看到。

1. Instagram的API只侦听HTTPS,而不是HTTP。标准http节点模块在此不起作用;您至少需要使用https

2.你定义一个名为post_data条件语句中的变量:

if(headers['Content-Type'] == "application/json"){ 
    post_options["json"] = true; 
    var post_data = JSON.stringify(body); 
}else 
    var post_data = querystring.stringify(body); 

我们已经谈过不是隐含括号乱搞(不要做到这一点),但除此之外,你'将局部变量的作用域定义为仅限于条件语句并用数据填充它。一旦条件结束,它就会被销毁。你可以在console.log(post_data)之后立即检查它 - 它将是空的。

3.对于Instagram OAuth流程有三个截然不同的步骤 - 它看起来像你试图(有点?)代理第一个重定向网址?但是,当它实际上是两个截然不同的终端时,您也会为这两个网站呈现相同的网址。它看起来像你只是从How to make an HTTP POST request in node.js?复制代码,而不完全理解它在做什么或为什么。最重要的是,工作curl(Instagram示例代码)的Content-Type,使用multipart/form-data,而不是x-www-form-urlencoded


解决方案

因为你实际上没有提供一个MCVE,我真的不能从断码你想做什么推断。我只能猜测,所以我打算给你一个解决方案,使用request来完成繁重的工作,所以你不必这样做。你会注意到代码的显着减少。下面是它的步骤:

  1. 生成一个隐含的补助链接
  2. 创建侦听重定向和捕捉授权码
  3. 使POST请求Instagram的检索令牌
服务器

在这里你去:

const querystring = require('querystring'), 
     http = require('http'), 
     request = require('request'), 
     url = require('url') 

// A manual step - you need to go here in your browser 
console.log('Open the following URL in your browser to authenticate and authorize your app:') 
console.log('https://api.instagram.com/oauth/authorize/?' + querystring.stringify({ 
    client_id: "90b2ec5599c74517a8493dad7eff13de", 
    redirect_uri: "http://localhost:8001", 
    response_type: "code", 
    scope: "public_content" 
})) 

// Create a server that listens for the OAuth redirect 
http.createServer(function(req, res) { 
    // Regrieve the query params from the redirect URI so we can get 'code' 
    var queryData = url.parse(req.url, true).query || {} 

    // Build form data for multipart/form-data POST request back to Instagram's API 
    var formData = { 
     client_id: "90b2ec5599c74517a8493dad7eff13de", 
     client_secret: "1b74d347702048c0847d763b0e266def", 
     grant_type: "authorization_code", 
     redirect_uri: "http://localhost:8001", 
     code: queryData.code || "" 
    } 

    // Send the POST request using 'request' module because why would you do it the hard way? 
    request.post({ 
     uri: "https://api.instagram.com/oauth/access_token", 
     formData: formData, 
     json: true 
    }, function(err, resp, body) { 

     // Write the response 
     console.log(body) 
     res.setHeader('Content-Type', "application/json") 
     res.end(JSON.stringify(body)) 
    }) 

}).listen(8001)