xdevplatform / Twitter-API-v2-sample-code

Sample code for the Twitter API v2 endpoints
https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api
Apache License 2.0
2.61k stars 967 forks source link

Twitter V2 Get Tweets API's Query Parameter not working #152

Open Prottoy2938 opened 4 months ago

Prottoy2938 commented 4 months ago

I'm following this twitter api documentation to get detailed information for a tweet, but I can't seem to get the full tweet details (retweet count, hearts, geotags etc etc) with this API. I'm only getting this response for a tweet from the API:

{
  edit_history_tweet_ids: [ '1766576440005333244' ],
  id: '1766576440005333244',
  text: 'RT @MOdesign19: Congratulations KTR. #saloneX #supportlocalfootball'
}

I've put the proper query parameters (like expansions, tweet.fields), but nothing seems to be getting me a detailed report of the tweet. This is what my code looks like:


const needle = require("needle");

const YOUR_BEARER_TOKEN = "MY-TOKEN-HERE";
const TWEET_ID = "1766576440005333244"; // Replace with the actual tweet ID

// Define the endpoint URL
const endpointUrl = `https://api.twitter.com/2/tweets/${TWEET_ID}`;

// Set up the request headers
const headers = {
  Authorization: `Bearer ${YOUR_BEARER_TOKEN}`,
  "Content-Type": "application/json",
};
// Specify the fields you want to retrieve
const tweetFields =
  "attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,non_public_metrics,public_metrics,organic_metrics,promoted_metrics,possibly_sensitive,referenced_tweets,reply_settings,source,text,withheld";

// Set up the request parameters including the desired fields
const params = {
  "tweet.fields":
    "attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,public_metrics,referenced_tweets,reply_settings,source,text,withheld",
  expansions: "author_id",
  "user.fields":
    "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,verified_type,withheld",
  "media.fields":
    "duration_ms,height,media_key,preview_image_url,type,url,width,public_metrics,alt_text,variants",
  "place.fields":
    "contained_within,country,country_code,full_name,geo,id,name,place_type",
  "poll.fields": "duration_minutes,end_datetime,id,options,voting_status",
};

async function helloWorld() {
  // Make the GET request
  const response = await needle("get", endpointUrl, params, { headers });
  console.log(response.body.data);
}

helloWorld();
Prottoy2938 commented 4 months ago

any update?