24

Twitter推文监控API

 1 year ago
source link: https://www.liesauer.net/blog/post/twitter-tweets-watching.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

阅读前须知

本文所提及的所有接口均非官方开放API,而是直接从网页端抓包而来,可能存在以下限制及风险!!!

  1. Cookie CSRF Token有效时间未知,可能会不定期失效或请求失败
  2. 大多数看似无用的参数、Cookie已被清理,并未附带在请求内,可能会被识别成脚本或机器人请求
  3. 监控大量账号效率低下(每个账号都要单独发送请求获取数据,无法做到类似官方的流式订阅)
  4. 不确定是否有封号风险
  5. 不确定请求频率风控策略

获取必需参数、Cookie

打开开发者面板并打开Web端Twitterhttps://mobile.twitter.com/home,找到以下请求(或直接搜索HomeTimeline):



  1. https://mobile.twitter.com/i/api/graphql/XXXXXXXXXX/HomeTimeline

Bearer Token

authorization请求头就是了。

Cookie ct0 auth_token

cookie请求头里把这两个找出来即可,别的不需要。

CSRF Token

x-csrf-token请求头就是了,实测CSRF Token可重用。

请求API

$XXX$替换为真实的参数即可,请求中的variables features参数都是JSON文本,把它们当成普通内容将其正常的urlencode传递到url即可,无需任何额外处理。

用户名转用户ID



  1. GET https://mobile.twitter.com/i/api/graphql/0dbpv_sJc_YxCb-1RO2NFA/UserByScreenName
  2. 请求头:
  3. authorization: $BEARER_TOKEN$
  4. content-type: application/json
  5. x-csrf-token: $CSRF_TOKEN$
  6. x-twitter-active-user: yes
  7. x-twitter-auth-type: OAuth2Session
  8. x-twitter-client-language: zh-cn
  9. 请求Cookie:
  10. ct0: $CT0$
  11. auth_token: $AUTH_TOKEN$
  12. 请求参数:
  13. variables: {"screen_name":"$USER_NAME$","withSafetyModeUserFields":true,"withSuperFollowsUserFields":true}
  14. features: {"responsive_web_twitter_blue_verified_badge_is_enabled":true,"verified_phone_label_enabled":false,"responsive_web_twitter_blue_new_verification_copy_is_enabled":true,"responsive_web_graphql_timeline_navigation_enabled":true}


  1. GET https://mobile.twitter.com/i/api/graphql/zEAmYdYUvRuhrr9zjc5Vvg/UserTweets
  2. 请求头:
  3. authorization: $BEARER_TOKEN$
  4. content-type: application/json
  5. x-csrf-token: $CSRF_TOKEN$
  6. x-twitter-active-user: yes
  7. x-twitter-auth-type: OAuth2Session
  8. x-twitter-client-language: zh-cn
  9. 请求Cookie:
  10. ct0: $CT0$
  11. auth_token: $AUTH_TOKEN$
  12. 请求参数:
  13. variables: {"userId":"$USER_ID$","count":40,"includePromotedContent":true,"withQuickPromoteEligibilityTweetFields":true,"withSuperFollowsUserFields":true,"withDownvotePerspective":false,"withReactionsMetadata":false,"withReactionsPerspective":false,"withSuperFollowsTweetFields":true,"withVoice":true,"withV2Timeline":true}
  14. features: {"responsive_web_twitter_blue_verified_badge_is_enabled":true,"verified_phone_label_enabled":false,"responsive_web_graphql_timeline_navigation_enabled":true,"view_counts_public_visibility_enabled":false,"view_counts_everywhere_api_enabled":false,"tweetypie_unmention_optimization_enabled":true,"responsive_web_uc_gql_enabled":true,"vibe_api_enabled":true,"responsive_web_edit_tweet_api_enabled":true,"graphql_is_translatable_rweb_tweet_is_translatable_enabled":true,"standardized_nudges_misinfo":true,"tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled":false,"interactive_text_enabled":true,"responsive_web_text_conversations_enabled":false,"responsive_web_enhance_cards_enabled":true}

在获取到首页40条推文后,返回的数据还包含了当前页的最后推文游标,搜索TimelineTimelineCursor字段,并找到cursorTypeBottom的内容,其value就是当前页的最后推文的游标,并将其加入到variables参数的cursor字段中即可获取后面分页内容。
此时variables内容如下:



  1. {"userId":"$USER_ID$","count":40,"cursor":"$BOTTOM_CURSOR$","includePromotedContent":true,"withQuickPromoteEligibilityTweetFields":true,"withSuperFollowsUserFields":true,"withDownvotePerspective":false,"withReactionsMetadata":false,"withReactionsPerspective":false,"withSuperFollowsTweetFields":true,"withVoice":true,"withV2Timeline":true}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK