Instagram API无法通过Nodejs工作



这是我在本地主机上运行的http POST代码:

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如下:

    正文["client_id"] = CLIENT_ID;

    正文["client_secret"] = CLIENT_SECRET;

    正文["grant_type"] = "authorization_code";

    正文["redirect_uri"] = AUTHORIZATION_REDIRECT_URI;

    正文["代码"] = login_code;

    正文["范围"] = "public_content";

  • headers= {} (空,假设标头['内容类型'] == 未定义为true)

  • 重要:sent_handler返回假

  • 控制台.log for "FINAL BODY"(变量post) 返回 "{}">

注意:使用 curl 与 api instagram 的通信有效。所以我真的相信问题出在 nodejs 中这段代码的某些部分。

有人知道吗?请询问是否需要更多信息

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

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 流程有三个不同的步骤 - 看起来您正在尝试(有点?但是,您也为这两个终结点提供了相同的URL,而实际上它是两个不同的终结点。看起来您刚刚从如何在node.js中发出HTTP POST请求中复制了代码?不太明白它在做什么或为什么。最重要的是,工作curl(Instagram示例代码)的Content-Type,使用multipart/form-data,而不是x-www-form-urlencoded


溶液

由于您实际上没有提供 MCVE,因此我无法从损坏的代码中推断出您要做什么。我只能猜测,所以我会给你一个解决方案,使用request来完成繁重的工作,所以你不必这样做。您会注意到代码大幅减少。以下是它执行的步骤:

  1. 生成隐式授权链接
  2. 创建侦听重定向并捕获身份验证代码的服务器
  3. 向 Instagram 发出 POST 请求以检索令牌

给你:

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)

最新更新