1+ import requests
2+ import json
3+
4+ api = requests .get (
5+ "https://raw.githubusercontent.com/fa0311/TwitterInternalAPIDocument/develop/docs/json/API.json"
6+ ).json ()
7+
8+ # Always use new APIs
9+ # api = requests.get("https://github.com/fa0311/TwitterInternalAPIDocument/blob/master/docs/json/API.json").json()
10+
11+ headers = api ["header" ]
12+ session = requests .session ()
13+ session .get ("https://twitter.com/" , headers = {"User-Agent" : headers ["User-Agent" ]})
14+ x_guest_token = session .post ("https://api.twitter.com/1.1/guest/activate.json" ,headers = headers ).json ()["guest_token" ]
15+ headers .update ({
16+ "Content-type" : "application/json" ,
17+ "x-guest-token" : x_guest_token ,
18+ "x-twitter-active-user" : "yes" ,
19+ "x-twitter-client-language" : "en" ,
20+ })
21+
22+ # <Recommendation> You can also use TwitterFrontendFlow
23+ # flow = TwitterFrontendFlow()
24+ # session = flow.session
25+ # x_guest_token = flow.x_guest_token
26+
27+ # API you want to use
28+ operationName = "UserTweets"
29+
30+ # variables must be entered by yourself
31+ variables = {
32+ "userId" : "900282258736545792" ,
33+ "count" : 40 ,
34+ "includePromotedContent" : True ,
35+ "withQuickPromoteEligibilityTweetFields" : True ,
36+ "withSuperFollowsUserFields" : True ,
37+ "withDownvotePerspective" : False ,
38+ "withReactionsMetadata" : False ,
39+ "withReactionsPerspective" : False ,
40+ "withSuperFollowsTweetFields" : True ,
41+ "withVoice" : True ,
42+ "withV2Timeline" : True ,
43+ }
44+
45+ data = api ["graphql" ][operationName ]
46+ parameters = {
47+ "queryId" : data ["queryId" ],
48+ "variables" : json .dumps (variables ),
49+ "features" : json .dumps (data ["features" ]),
50+ }
51+
52+ data ["url" ] = "https://api.twitter.com/graphql/oPHs3ydu7ZOOy2f02soaPA/UserTweets"
53+
54+
55+ if data ["method" ] == "GET" :
56+ response = session .get (data ["url" ],headers = headers , params = parameters ).json ()
57+ elif data ["method" ] == "POST" :
58+ response = session .post (data ["url" ],headers = headers , json = parameters ).json ()
59+
60+
61+ print (response )
0 commit comments