Тема: AHK: Авторизация в твиттере twitter
Пример кода получения твитов через поиск (нужна авторизация).
Для того, чтобы каждый раз не логиниться, после успешного логина стоит куда-нибудь сохранить csrf_token и auth_token и при следующем запуске скрипта их использовать.
username := "username"
email := "email@gmail.com"
password := "password"
authorization := "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA" ; twitter always uses exactly this one
auth_token = 0a0205c12365f6355613b6f2d486d474b6 ; if wrong, will be generated by script
csrf_token = b8caffd5c8d32770003427c21416baa0daff78c ; if wrong, will be generated by script
WinHTTP := ComObjCreate("WinHTTP.WinHttpRequest.5.1")
getTweets:
url := "https://twitter.com/i/api/2/search/adaptive.json?include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&include_ext_has_nft_avatar=1&include_ext_is_blue_verified=1&include_ext_verified_type=1&include_ext_profile_image_shape=1&skip_status=1&cards_platform=Web-12&include_cards=1&include_ext_alt_text=true&include_ext_limited_action_results=false&include_quote_count=true&include_reply_count=1&tweet_mode=extended&include_ext_views=true&include_entities=true&include_user_entities=true&include_ext_media_color=true&include_ext_media_availability=true&include_ext_sensitive_media_warning=true&include_ext_trusted_friends_metadata=true&send_error_codes=true&simple_quoted_tweet=true&q=from%3AhtGOIW%20since%3A2009-11-01%20until%3A2011-01-01&tweet_search_mode=live&query_source=typed_query&count=20&requestContext=launch&pc=1&spelling_corrections=1&include_ext_edit_control=true&ext=mediaStats%2ChighlightedLabel%2ChasNftAvatar%2CvoiceInfo%2CbirdwatchPivot%2Cenrichments%2CsuperFollowMetadata%2CunmentionInfo%2CeditControl%2Cvibe"
WinHTTP.Open("GET", url, true)
WinHTTP.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
WinHTTP.SetRequestHeader("Pragma", "no-cache")
WinHTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
WinHTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
WinHTTP.SetRequestHeader("authorization", authorization)
WinHTTP.SetRequestHeader("x-csrf-token", csrf_token)
WinHTTP.SetRequestHeader("cookie", "auth_token=" auth_token "; ct0=" csrf_token)
WinHTTP.Send()
WinHTTP.WaitForResponse()
if RegexMatch(WinHTTP.ResponseText, "^\{""errors""")
{
GoSub login
GoTo getTweets
}
msgbox % WinHTTP.ResponseText
return
login:
WinHTTP.Open("GET", "https://twitter.com/i/flow/login", true)
WinHTTP.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
WinHTTP.SetRequestHeader("Pragma", "no-cache")
WinHTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
WinHTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
WinHTTP.Send()
WinHTTP.WaitForResponse()
RegexMatch(WinHTTP.ResponseText, "s)document\.cookie=""gt=(.+?);", match)
guest_token := match1
body = {"input_flow_data":{"flow_context":{"debug_overrides":{},"start_location":{"location":"manual_link"}}},"subtask_versions":{"action_list":2,"alert_dialog":1,"app_download_cta":1,"check_logged_in_account":1,"choice_selection":3,"contacts_live_sync_permission_prompt":0,"cta":7,"email_verification":2,"end_flow":1,"enter_date":1,"enter_email":2,"enter_password":5,"enter_phone":2,"enter_recaptcha":1,"enter_text":5,"enter_username":2,"generic_urt":3,"in_app_notification":1,"interest_picker":3,"js_instrumentation":1,"menu_dialog":1,"notifications_permission_prompt":2,"open_account":2,"open_home_timeline":1,"open_link":1,"phone_verification":4,"privacy_options":1,"security_key":3,"select_avatar":4,"select_banner":2,"settings_list":7,"show_code":1,"sign_up":2,"sign_up_review":4,"tweet_selection_urt":1,"update_users":1,"upload_media":1,"user_recommendations_list":4,"user_recommendations_urt":1,"wait_spinner":3,"web_modal":1}}
WinHTTP.Open("POST", "https://api.twitter.com/1.1/onboarding/task.json?flow_name=login", true)
WinHTTP.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
WinHTTP.SetRequestHeader("Pragma", "no-cache")
WinHTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
WinHTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
WinHTTP.SetRequestHeader("content-type", "application/json")
WinHTTP.SetRequestHeader("authorization", authorization)
WinHTTP.SetRequestHeader("x-guest-token", guest_token)
WinHTTP.Send(body)
WinHTTP.WaitForResponse()
RegexMatch(WinHTTP.ResponseText, "s)""flow_token"":""(.+?)""", match)
flow_token := match1
body = {"flow_token":"%flow_token%","subtask_inputs":[]}
WinHTTP.Open("POST", "https://api.twitter.com/1.1/onboarding/task.json", true)
WinHTTP.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
WinHTTP.SetRequestHeader("Pragma", "no-cache")
WinHTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
WinHTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
WinHTTP.SetRequestHeader("content-type", "application/json")
WinHTTP.SetRequestHeader("authorization", authorization)
WinHTTP.SetRequestHeader("x-guest-token", guest_token)
WinHTTP.Send(body)
WinHTTP.WaitForResponse()
RegexMatch(WinHTTP.ResponseText, "s)""flow_token"":""(.+?)""", match)
flow_token := match1
body = {"flow_token":"%flow_token%","subtask_inputs":[{"subtask_id":"LoginEnterUserIdentifierSSO","settings_list":{"setting_responses":[{"key":"user_identifier","response_data":{"text_data":{"result":"%username%"}}}],"link":"next_link"}}]}
WinHTTP.Open("POST", "https://api.twitter.com/1.1/onboarding/task.json", true)
WinHTTP.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
WinHTTP.SetRequestHeader("Pragma", "no-cache")
WinHTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
WinHTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
WinHTTP.SetRequestHeader("content-type", "application/json")
WinHTTP.SetRequestHeader("authorization", authorization)
WinHTTP.SetRequestHeader("x-guest-token", guest_token)
WinHTTP.Send(body)
WinHTTP.WaitForResponse()
RegexMatch(WinHTTP.ResponseText, "s)""flow_token"":""(.+?)""", match)
flow_token := match1
if InStr(WinHTTP.ResponseText, "LoginEnterAlternateIdentifierSubtask")
{
body = {"flow_token":"%flow_token%","subtask_inputs":[{"subtask_id":"LoginEnterAlternateIdentifierSubtask","enter_text":{"text":"%email%","link":"next_link"}}]}
WinHTTP.Open("POST", "https://api.twitter.com/1.1/onboarding/task.json", true)
WinHTTP.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
WinHTTP.SetRequestHeader("Pragma", "no-cache")
WinHTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
WinHTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
WinHTTP.SetRequestHeader("content-type", "application/json")
WinHTTP.SetRequestHeader("authorization", authorization)
WinHTTP.SetRequestHeader("x-guest-token", guest_token)
WinHTTP.Send(body)
WinHTTP.WaitForResponse()
RegexMatch(WinHTTP.ResponseText, "s)""flow_token"":""(.+?)""", match)
flow_token := match1
}
body = {"flow_token":"%flow_token%","subtask_inputs":[{"subtask_id":"LoginEnterPassword","enter_password":{"password":"%password%","link":"next_link"}}]}
WinHTTP.Open("POST", "https://api.twitter.com/1.1/onboarding/task.json", true)
WinHTTP.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
WinHTTP.SetRequestHeader("Pragma", "no-cache")
WinHTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
WinHTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
WinHTTP.SetRequestHeader("content-type", "application/json")
WinHTTP.SetRequestHeader("authorization", authorization)
WinHTTP.SetRequestHeader("x-guest-token", guest_token)
WinHTTP.Send(body)
WinHTTP.WaitForResponse()
RegexMatch(WinHTTP.ResponseText, "s)""flow_token"":""(.+?)""", match)
flow_token := match1
body = {"flow_token":"%flow_token%","subtask_inputs":[{"subtask_id":"AccountDuplicationCheck","check_logged_in_account":{"link":"AccountDuplicationCheck_false"}}]}
WinHTTP.Open("POST", "https://api.twitter.com/1.1/onboarding/task.json", true)
WinHTTP.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
WinHTTP.SetRequestHeader("Pragma", "no-cache")
WinHTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
WinHTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
WinHTTP.SetRequestHeader("content-type", "application/json")
WinHTTP.SetRequestHeader("authorization", authorization)
WinHTTP.SetRequestHeader("x-guest-token", guest_token)
WinHTTP.Send(body)
WinHTTP.WaitForResponse()
RegexMatch(WinHTTP.getallresponseheaders, "s)Set-Cookie: ct0=(.+?);.+?Set-Cookie: auth_token=(.+?);", match)
csrf_token := match1, auth_token := match2
WinHTTP.Open("GET", "https://twitter.com/i/api/graphql/XeaGEEWXp54Km4mVNvcMmg/Viewer?variables=%7B%22withCommunitiesMemberships%22%3Atrue%2C%22withSubscribedTab%22%3Atrue%2C%22withCommunitiesCreation%22%3Atrue%7D&features=%7B%22blue_business_profile_image_shape_enabled%22%3Atrue%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%7D", true)
WinHTTP.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
WinHTTP.SetRequestHeader("Pragma", "no-cache")
WinHTTP.SetRequestHeader("Cache-Control", "no-cache, no-store")
WinHTTP.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
WinHTTP.SetRequestHeader("authorization", authorization)
WinHTTP.SetRequestHeader("x-csrf-token", csrf_token)
WinHTTP.Send()
WinHTTP.WaitForResponse()
RegexMatch(WinHTTP.getallresponseheaders, "s)Set-Cookie: ct0=(.+?);", match)
csrf_token := match1
msgbox % "auth_token = " auth_token "`ncsrf_token = " csrf_token
return