Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid use of 'since_id' or 'until_id' in conjunction with 'start_time' or 'end_time'. #152

Closed
highku opened this issue Jan 7, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@highku
Copy link

highku commented Jan 7, 2022

Get Recent Tweets iterator throws an error when the request contains a date filter.

        try {
            let resp = await roClient.v2.search(
                text,
                {
                    'max_results': 100,
                    'tweet.fields': 'created_at,in_reply_to_user_id,geo,author_id,conversation_id,context_annotations,lang,text,source,public_metrics,withheld,possibly_sensitive',
                    'place.fields': 'name,country_code',
                    'user.fields': 'created_at,username,name,verified,public_metrics,profile_image_url',
                    'expansions': 'author_id,geo.place_id,in_reply_to_user_id',
                    'start_time': startTimeStr
                })
            for await (const tweet of resp) {
                console.log("Tweet: " + JSON.stringify(tweet))
                tweets.push(tweet)
                totalRetweets += tweet.public_metrics.retweet_count
                totalLikes += tweet.public_metrics.like_count
    
                const authorUser = resp.includes.author(tweet);
    
                if (!authorsMap.has(tweet.author_id)) {
                    authorsMap.set(tweet.author_id, { author: authorUser, tweets: 1 })
                } else {
                    authorsMap.get(tweet.author_id).tweets += 1
                }
            }
    
            tweets.map((tweet) => {
                allTweetsText = allTweetsText.concat(tweet.text)
                allTweetsText = allTweetsText.concat("\n")
            })
        } catch (error) {
            console.error("Error while fetching tweets. ")
            console.error(JSON.stringify(error))
        }

Error thrown:

{
  "type": "response",
  "code": 400,
  "error": {
    "errors": [
      {
        "parameters": {
          "until_id": [
            "1479477826428358657"
          ],
          "start_time": [
            "2022-01-06T20:27Z"
          ]
        },
        "message": "Invalid use of 'since_id' or 'until_id' in conjunction with 'start_time' or 'end_time'."
      }
    ],
    "title": "Invalid Request",
    "detail": "One or more parameters to your request was invalid.",
    "type": "https://api.twitter.com/2/problems/invalid-request"
  },
  ...
}
@alkihis alkihis added the bug Something isn't working label Jan 8, 2022
@alkihis
Copy link
Collaborator

alkihis commented Jan 10, 2022

Hi,

This is quite a strange choice from Twitter to deny use of until_id and start_time together, as they're not exclusive filters 😅.

I'll look how to paginate without use of since_id/until_id, but the docs seems to not mention clearly pagination tokens for this endpoint. next_token is mentionned in response field list, but is marked as optional.

As a mitigation, you can approximate a start_time using Twitter snowflake IDs (as they are encoded with a timestamp inside):

const TWITTER_START_EPOCH = 1288834974657n;

function dateToTwitterSnowflake(date) {
  const dateTimestamp = BigInt(date.valueOf());
  return ((dateTimestamp - TWITTER_START_EPOCH) << 22n).toString();
}

then use start_time as since_id:

const resp = await roClient.v2.search(text, { since_id: dateToTwitterSnowflake(new Date(startTimeStr)) })

@alkihis
Copy link
Collaborator

alkihis commented Jan 10, 2022

Fix now available in 1.9.1 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants