Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix X (Twitter) login issue: resolved authentication error #173

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
87 changes: 75 additions & 12 deletions twikit/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(

self._token = TOKEN
self._user_id = None
self._user_agent = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
self._user_agent = ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/122.0.0.0 Safari/537.36')
self._act_as = None
Expand Down Expand Up @@ -225,11 +225,11 @@ def _base_headers(self) -> dict[str, str]:
Base headers for Twitter API requests.
"""
headers = {
'authorization': f'Bearer {self._token}',
'content-type': 'application/json',
'Authorization': f'Bearer {self._token}',
'Content-Type': 'application/json',
'X-Twitter-Auth-Type': 'OAuth2Session',
'X-Twitter-Active-User': 'yes',
'Referer': 'https://twitter.com/',
'Referer': 'https://x.com/',
'User-Agent': self._user_agent,
}

Expand Down Expand Up @@ -293,8 +293,66 @@ async def login(

flow = Flow(self, guest_token)

await flow.execute_task(params={'flow_name': 'login'})
await flow.execute_task()
await flow.execute_task(input_flow_data={
"input_flow_data": {
"flow_context": {
"debug_overrides": {},
"start_location": {
"location": "splash_screen"
}
}
},
"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
}
},params={'flow_name': 'login'})
flow_token = flow.token

await flow.execute_task(flow_token=flow_token, sso={"provider": "apple"})
# await flow.execute_task({"subtask_id":"LoginJsInstrumentationSubtask","js_instrumentation":{"response":""},"link":"next_link"},flow_token=flow_token)
await flow.execute_task({"subtask_id":"LoginJsInstrumentationSubtask","js_instrumentation":{"response":"{\"rf\":{\"a4fc506d24bb4843c48a1966940c2796bf4fb7617a2d515ad3297b7df6b459b6\":121,\"bff66e16f1d7ea28c04653dc32479cf416a9c8b67c80cb8ad533b2a44fee82a3\":-1,\"ac4008077a7e6ca03210159dbe2134dea72a616f03832178314bb9931645e4f7\":-22,\"c3a8a81a9b2706c6fec42c771da65a9597c537b8e4d9b39e8e58de9fe31ff239\":-12},\"s\":\"ZHYaDA9iXRxOl2J3AZ9cc23iJx-Fg5E82KIBA_fgeZFugZGYzRtf8Bl3EUeeYgsK30gLFD2jTQx9fAMsnYCw0j8ahEy4Pb5siM5zD6n7YgOeWmFFaXoTwaGY4H0o-jQnZi5yWZRAnFi4lVuCVouNz_xd2BO2sobCO7QuyOsOxQn2CWx7bjD8vPAzT5BS1mICqUWyjZDjLnRZJU6cSQG5YFIHEPBa8Kj-v1JFgkdAfAMIdVvP7C80HWoOqYivQR7IBuOAI4xCeLQEdxlGeT-JYStlP9dcU5St7jI6ExyMeQnRicOcxXLXsan8i5Joautk2M8dAJFByzBaG4wtrPhQ3QAAAZEi-_t7\"}","link":"next_link"}},flow_token=flow_token)
flow_token = flow.token

await flow.execute_task({
'subtask_id': 'LoginEnterUserIdentifierSSO',
'settings_list': {
Expand All @@ -308,7 +366,8 @@ async def login(
],
'link': 'next_link'
}
})
},flow_token=flow_token)
flow_token = flow.token

if flow.task_id == 'LoginEnterAlternateIdentifierSubtask':
await flow.execute_task({
Expand All @@ -317,30 +376,34 @@ async def login(
'text': auth_info_2,
'link': 'next_link'
}
})
},flow_token=flow_token)
flow_token = flow.token

await flow.execute_task({
'subtask_id': 'LoginEnterPassword',
'enter_password': {
'password': password,
'link': 'next_link'
}
})
},flow_token=flow_token)
flow_token = flow.token

if flow.task_id == 'DenyLoginSubtask':
#raise TwitterException(flow.response)
raise TwitterException(flow.response['subtasks'][0]['cta']['secondary_text']['text'])

await flow.execute_task({
'subtask_id': 'AccountDuplicationCheck',
'check_logged_in_account': {
'link': 'AccountDuplicationCheck_false'
}
})
},flow_token=flow_token)

if not flow.response['subtasks']:
return

self._user_id = find_dict(flow.response, 'id_str', find_one=True)[0]
flow_token = flow.token

if flow.task_id == 'LoginTwoFactorAuthChallenge':
if totp_secret is None:
Expand All @@ -355,7 +418,7 @@ async def login(
'text': totp_code,
'link': 'next_link'
}
})
},flow_token=flow_token)

if flow.task_id == 'LoginAcid':
print(find_dict(flow.response, 'secondary_text', find_one=True)[0]['text'])
Expand All @@ -366,7 +429,7 @@ async def login(
'text': input('>>> '),
'link': 'next_link'
}
})
},flow_token=flow_token)

return flow.response

Expand Down
25 changes: 18 additions & 7 deletions twikit/client/v11.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Endpoint:
GUEST_ACTIVATE = 'https://api.twitter.com/1.1/guest/activate.json'
ACCOUNT_LOGOUT = 'https://api.twitter.com/1.1/account/logout.json'
ONBOARDING_TASK = 'https://api.twitter.com/1.1/onboarding/task.json'
ONBOARDING_SSO_TASK = 'https://api.x.com/1.1/onboarding/sso_init.json'
SETTINGS = 'https://api.twitter.com/1.1/account/settings.json'
UPLOAD_MEDIA = 'https://upload.twitter.com/i/media/upload.json'
UPLOAD_MEDIA_2 = 'https://upload.twitter.com/i/media/upload2.json'
Expand Down Expand Up @@ -69,19 +70,29 @@ async def account_logout(self):

async def onboarding_task(self, guest_token, token, subtask_inputs, **kwargs):
data = {}
if token is not None:
data['flow_token'] = token
if subtask_inputs is not None:
endpoint_url = Endpoint.ONBOARDING_TASK

if kwargs.get('flow_token') is not None:
data['flow_token'] = kwargs.pop('flow_token')
if subtask_inputs is not None and kwargs.get('input_flow_data') is None:
data['subtask_inputs'] = subtask_inputs

if kwargs.get('input_flow_data') is not None:
data = kwargs.pop('input_flow_data')
if kwargs.get('sso') is not None:
data = kwargs.pop('sso')
endpoint_url = Endpoint.ONBOARDING_SSO_TASK


headers = self.base._base_headers | {
'x-guest-token': guest_token
'X-Guest-Token': guest_token
}
headers.pop('X-Twitter-Active-User')
headers.pop('X-Twitter-Auth-Type')

#headers.pop('X-Twitter-Active-User')
headers.pop('X-Twitter-Auth-Type')

return await self.base.post(
Endpoint.ONBOARDING_TASK,
endpoint_url,
json=data,
headers=headers,
**kwargs
Expand Down