Twitter api用node.js返回空体

时间:2016-06-20 02:37:38

标签: node.js express twitter

我正在尝试访问twitter API以从主题标签中获取帖子。我按照仅应用程序身份验证的步骤。第2步是获得我能够做的持有人令牌。然而,在该步骤之后,当我发送获取请求以获取推文时,我收到一个空的身体。

在下面的代码中,我省略了API密钥本身。

    var express = require('express');
    var bodyParser = require('body-parser');

    //var sendNotification = require('./utils/sendNotification.js');
    var request = require('request');
    var app = express();

    app.use(bodyParser.urlencoded({
          extended: true
    }));
    app.use(bodyParser.json());



    function getTweets(){
         request.post({
                headers: {'content-type' : 'application/x-www-form-urlencoded',"Authorization": 'Basic WmR5Tk9tMHJQUlV3VVlwZm1iRDR3eTRCeTpFdVRoZ2FPSEZpNDJsM0h6azlYWjJwaWNCaFA2SFh0RnBDdkNycDN2Zk1zM29TV2NGYw=='},
                url:     'https://api.twitter.com/oauth2/token',
                body: 'grant_type=client_credentials'
         }, function(error, response, body){
                var token =JSON.parse(body)['access_token'];
                console.log("Bearer "+ token);
                request.get({
                      headers: {'Authorization':'Bearer ' + JSON.stringify(token)},
                      url: 'https://api.twitter.com/1.1/search/tweets.json?',
                      body: encodeURIComponent('q=#dubNations')
                },function(e,r,b){
                   console.log(r)
                  console.log(b)
                });
         });

   }

部分输出(响应)是:

     ClientRequest {
    domain: null,
    _events: [Object],
    _eventsCount: 5,
    _maxListeners: undefined,
    output: [],
    outputEncodings: [],
    outputCallbacks: [],
    outputSize: 0,
    writable: true,
    _last: true,
    chunkedEncoding: false,
    shouldKeepAlive: false,
    useChunkedEncodingByDefault: false,
    sendDate: false,
    _removedHeader: [Object],
    _contentLength: null,
    _hasBody: true,
    _trailer: '',
    finished: true,
    _headerSent: true,
    socket: [Object],
    connection: [Object],
    _header: 'GET /1.1/search/tweets.json? HTTP/1.1\r\nAuthorization: Bearer "AAAAAAAAAAAAAAAAAAAAAMzZvgAAAAAATAmG%2Fj8Hnx3oqys3DqfsfbuSuIU%3DVUfy6IOBmx5QPrvhHybuLSHcG8fE8sO27r1MjX1YZ2Ar4ynwHz"\r\nhost: api.twitter.com\r\ncontent-length: 17\r\nConnection: close\r\n\r\n',
    _headers: [Object],
    _headerNames: [Object],
    _onPendingData: null,
    agent: [Object],
    socketPath: undefined,
    method: 'GET',
    path: '/1.1/search/tweets.json?',
    _ended: true,
    parser: null,
    res: [Circular] },
 ntick: true,
 response: [Circular],
 originalHost: 'api.twitter.com',
 originalHostHeaderName: 'host',
 responseContent: [Circular],
 _ended: true,
 _callbackCalled: true },
 toJSON: [Function: responseToJSON],
 caseless: 
  Caseless {
    dict: 
     { connection: 'close',
      'content-length': '0',
    date: 'Mon, 20 Jun 2016 02:22:03 GMT',
    server: 'tsa_a',
    'x-connection-hash': '6cd802f0e295a3ad66ee45459323b83c' } },
read: [Function],
 body: '' }

如果您需要更多,请询问。

我不确定为什么主体是空的,因为正确地返回了承载令牌,而我只是在做一个主题标签的获取请求。

1 个答案:

答案 0 :(得分:0)

您的GET请求语法错误(正如@Brant所指出的那样)。您应该使用qs属性:

request.get({
    headers: {'Authorization':'Bearer ' + JSON.stringify(token)},
    url: 'https://api.twitter.com/1.1/search/tweets.json?',
    json: true,
    qs: {
        q: '#dubNations'
    }
},function(e,r,b){
    console.log(r)
    console.log(b)
});

我还添加了json: true属性,因为这会告诉请求将响应解析为JSON并将其返回到b参数中。