HunterLarco / twitter-v2

An asynchronous client library for the Twitter REST and Streaming API's
MIT License
163 stars 35 forks source link

Searching tweets/search/recent #77

Open pjlamb12 opened 3 years ago

pjlamb12 commented 3 years ago

Prerequisites

Description

I'm trying to search the recent tweets for a specific user. Here's the URL if I use Insomnia:

https://api.twitter.com/2/tweets/search/recent?start_time=2021-04-23T00:00:00Z&query=from%3A_TWITTER_HANDLE_GOES_HERE&tweet.fields=id,author_id,text

and GET on that URL returns data. This does not work:

twitterClient.get('tweets/search/recent', {
        query: 'from%3Athe_churchnews',
        start_time: '2021-04-23T00:00:00Z',
    });

In addition, the commented out parts of the above object cause an error, which is why they are commented out. I've looked at the source code to try and build that object correctly, but haven't been able to get it to work properly.

In addition, in the URL string I have the tweet.fields query param. I tried adding it to the options object in both of the following ways, with no success. The first way causes an error, the second way there is no error but I'm not sure if it's working since I don't get any data back.

tweet: {
    fields: 'id,author_id,text',
},

// And

'tweet.fields': 'id,author_id,text',

Any docs or pointers here would be greatly appreciated.

Steps to Reproduce

async function getTweets() {
    const dt = new Date();
    dt.setHours(dt.getHours() - 12);

    const dateString = dt.toISOString();

    const { data } = await twitterClient.get('tweets/search/recent', {
        query: 'from%3A_twitter_handle',
        start_time: dateString,
        'tweet.fields': 'id,author_id,text',
    });
    console.log(data);
    return data;
}

Expected behavior:

Tweets should print to the console

Actual behavior: undefined is printed to the console.

Reproduces how often: Every time

Versions

v1.1.0

Additional Information

pbshgthm commented 3 years ago

The options are url encoded using node url library, hence you shouldn't use from%3Athe_churchnews and use from:the_churchnews instead. Hope that helps.

However, if you need to use from:abc to:xyz it results in an error as the space character is encoded as + instead of %20. Does anyone know the fix for that?