GET 400(错误请求)与 js 中的节点

GET 400 (Bad Request) with node in js

本文关键字:js 节点 错误 请求 GET      更新时间:2023-09-26

我有这个 js

    var https = require('https');
    var contador =process.argv[2]?process.argv[2]:'...';
    var screen_name =process.argv[3]?process.argv[3]:'...';
    var user_id =process.argv[4]?process.argv[4]:'...';
    var options = {
        host: 'api.twitter.com',
        path: '/1.1/followers/ids.json/count/'+contador+'/screen_name/'+screen_name+'/user_id/'+user_id,
        method: 'GET'
    };
var req = https.request(options, function(res) {
    console.log('Response is '+res.statusCode);
                         res.setEncoding('utf8');
                         res.on('data', function (datos_JSON) {
                                 var datos=JSON.parse(datos_JSON);
                 });
 });

req.end();

我收到以下错误,响应 400,为什么?...

您有许多问题:

  1. 文档指出需要提供user_name或screen_name,而不是两者兼而有之。
  2. 这些内容将作为查询字符串参数提供,而不是路径参数
  3. 您需要先进行身份验证。Twitter API 不是开放的,你通常需要代表经过身份验证的用户进行操作(尽管也有可用的应用程序身份验证)。

下面是文档中的示例 URL:https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=sitestreams&count=5000

  • 阅读有关关注者 API 的文档:https://dev.twitter.com/docs/api/1.1/get/followers/ids
  • 了解身份验证:https://dev.twitter.com/docs/auth(适用于用户)和 https://dev.twitter.com/docs/auth/application-only-auth(适用于应用程序)