You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Twitter UserTweets API获取最新推文遇乱序缺失问题求助

排查Twitter UserTweets API返回数据混乱、无法获取最新推文的问题

核心问题分析

以下是具体的排查方向和修复步骤:

1. 修正GraphQL请求变量,过滤推广内容并强制获取最新推文

你的请求变量里includePromotedContent=true会混入推广推文,直接导致数据混乱;同时仅设置count=20未指定分页起点,默认返回的可能不是最新批次的推文。

调整variables参数:

  • 把includePromotedContent设为false,过滤推广内容
  • 添加cursor: null,强制请求最新的推文批次(首次请求用null,后续分页用返回的next cursor)
  • 关闭无关字段withQuickPromoteEligibilityTweetFields以简化请求

修改后的variables解码后示例:

{
  "userId": "目标用户ID",
  "count": 20,
  "includePromotedContent": false,
  "withQuickPromoteEligibilityTweetFields": false,
  "withVoice": true,
  "withV2Timeline": true,
  "cursor": null
}

2. 确保Guest Token的实时有效性

Guest Token是临时会话凭证,有效期仅几分钟,且和浏览器会话绑定:

  • 直接从浏览器Network面板当前UserTweets请求的x-guest-token头复制,不要使用过期或自行生成的token
  • 每次发起请求前重新获取最新的Guest Token

3. 补全关键请求头,匹配浏览器环境

你的请求头缺少几个浏览器自动携带的核心字段,可能导致API返回差异化内容:

  • 添加x-csrf-token:从浏览器cookie中提取ct0字段的值,需和Guest Token配合使用
  • 修改Referer为目标用户主页,比如https://x.com/目标用户名,而非根域名
  • 可选:添加浏览器当前会话的完整cookie头,确保请求环境完全匹配

4. 精准解析GraphQL响应结构

Twitter的API返回嵌套结构,直接遍历会混入非推文内容(如加载提示),需精准提取有效数据:

  • 核心数据在data.user.result.timeline_v2.timeline.instructions数组中
  • 过滤出type="TimelineAddEntries"的指令,再筛选entryId以tweet-开头的条目,这些才是真实推文
  • 推文详情在content.itemContent.tweet_results.result中

修改后的示例代码

const userId = "目标用户ID";
const targetUsername = "目标用户名";

// 编码请求变量
const variables = encodeURIComponent(JSON.stringify({
  userId: userId,
  count: 20,
  includePromotedContent: false,
  withQuickPromoteEligibilityTweetFields: false,
  withVoice: true,
  withV2Timeline: true,
  cursor: null
}));

// 保持features和fieldToggles与浏览器一致
const features = encodeURIComponent(JSON.stringify({
  "rweb_tipjar_consumption_enabled": true,
  "responsive_web_graphql_exclude_directive_enabled": true,
  "verified_phone_label_enabled": false,
  "creator_subscriptions_tweet_preview_api_enabled": true,
  "responsive_web_graphql_timeline_navigation_enabled": true,
  "responsive_web_graphql_skip_user_profile_image_extensions_enabled": false,
  "communities_web_enable_tweet_community_results_fetch": true,
  "c9s_tweet_anatomy_moderator_badge_enabled": true,
  "articles_preview_enabled": true,
  "tweetypie_unmention_optimization_enabled": true,
  "responsive_web_edit_tweet_api_enabled": true,
  "graphql_is_translatable_rweb_tweet_is_translatable_enabled": true,
  "view_counts_everywhere_api_enabled": true,
  "longform_notetweets_consumption_enabled": true,
  "responsive_web_twitter_article_tweet_consumption_enabled": true,
  "tweet_awards_web_tipping_enabled": false,
  "creator_subscriptions_quote_tweet_preview_enabled": false,
  "freedom_of_speech_not_reach_fetch_enabled": true,
  "standardized_nudges_misinfo": true,
  "tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled": true,
  "rweb_video_timestamps_enabled": true,
  "longform_notetweets_rich_text_read_enabled": true,
  "longform_notetweets_inline_media_enabled": true,
  "responsive_web_enhance_cards_enabled": false
}));

const fieldToggles = encodeURIComponent(JSON.stringify({
  "withArticlePlainText": false
}));

// 从浏览器Network面板复制最新的凭证
const token = "实时Guest Token";
const csrfToken = "cookie中的ct0值";

const res = await fetch(
  `https://api.x.com/graphql/V7H0Ap3_Hh2FyS75OCDO3Q/UserTweets?variables=${variables}&features=${features}&fieldToggles=${fieldToggles}`,
  {
    headers: {
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0",
      accept: "*/*",
      "accept-language": "en-US,en;q=0.9",
      authorization: "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
      "content-type": "application/json",
      priority: "u=1, i",
      "sec-ch-ua": "\"Chromium\";v=\"128\", \"Not;A=Brand\";v=\"24\", \"Microsoft Edge\";v=\"128\"",
      "sec-ch-ua-mobile": "?0",
      "sec-ch-ua-platform": "\"Windows\"",
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-origin",
      "x-guest-token": token,
      "x-csrf-token": csrfToken,
      "x-twitter-active-user": "yes",
      "x-twitter-client-language": "en",
      Referer: `https://x.com/${targetUsername}`,
      "Referrer-Policy": "strict-origin-when-cross-origin",
      "cookie": "浏览器当前会话的完整cookie字符串"
    },
    method: "GET"
  });

// 提取有效推文数据
const data = await res.json();
const tweetEntries = data.data.user.result.timeline_v2.timeline.instructions
  .filter(inst => inst.type === "TimelineAddEntries")
  .flatMap(inst => inst.entries)
  .filter(entry => entry.entryId.startsWith("tweet-"));

const filteredTweets = tweetEntries.map(entry => {
  const tweet = entry.content.itemContent.tweet_results.result;
  return {
    id: tweet.rest_id,
    createdAt: tweet.legacy.created_at,
    text: tweet.legacy.full_text || tweet.legacy.text
  };
});

console.log(filteredTweets);

额外注意事项

  • Twitter私有API无官方文档,接口哈希(如V7H0Ap3_Hh2FyS75OCDO3Q)随时可能变更,失效时需重新从浏览器Network面板复制最新路径
  • 频繁请求易触发IP限制,建议添加请求间隔

内容的提问来源于stack exchange,提问作者LordTakahiro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 18:42:02