Twitter推文监控API
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.
阅读前须知
本文所提及的所有接口均非官方开放API,而是直接从网页端抓包而来,可能存在以下限制及风险!!!
Cookie
CSRF Token
有效时间未知,可能会不定期失效或请求失败- 大多数看似无用的参数、Cookie已被清理,并未附带在请求内,可能会被识别成脚本或机器人请求
- 监控大量账号效率低下(每个账号都要单独发送请求获取数据,无法做到类似官方的流式订阅)
- 不确定是否有封号风险
- 不确定请求频率风控策略
获取必需参数、Cookie
打开开发者面板并打开Web端Twitterhttps://mobile.twitter.com/home
,找到以下请求(或直接搜索HomeTimeline
):
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
GET https://mobile.twitter.com/i/api/graphql/0dbpv_sJc_YxCb-1RO2NFA/UserByScreenName
请求头:
authorization: $BEARER_TOKEN$
content-type: application/json
x-csrf-token: $CSRF_TOKEN$
x-twitter-active-user: yes
x-twitter-auth-type: OAuth2Session
x-twitter-client-language: zh-cn
请求Cookie:
ct0: $CT0$
auth_token: $AUTH_TOKEN$
请求参数:
variables: {"screen_name":"$USER_NAME$","withSafetyModeUserFields":true,"withSuperFollowsUserFields":true}
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}
GET https://mobile.twitter.com/i/api/graphql/zEAmYdYUvRuhrr9zjc5Vvg/UserTweets
请求头:
authorization: $BEARER_TOKEN$
content-type: application/json
x-csrf-token: $CSRF_TOKEN$
x-twitter-active-user: yes
x-twitter-auth-type: OAuth2Session
x-twitter-client-language: zh-cn
请求Cookie:
ct0: $CT0$
auth_token: $AUTH_TOKEN$
请求参数:
variables: {"userId":"$USER_ID$","count":40,"includePromotedContent":true,"withQuickPromoteEligibilityTweetFields":true,"withSuperFollowsUserFields":true,"withDownvotePerspective":false,"withReactionsMetadata":false,"withReactionsPerspective":false,"withSuperFollowsTweetFields":true,"withVoice":true,"withV2Timeline":true}
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
字段,并找到cursorType
为Bottom
的内容,其value
就是当前页的最后推文的游标,并将其加入到variables
参数的cursor
字段中即可获取后面分页内容。
此时variables
内容如下:
{"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}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK