Skip to content

Commit 1ce9353

Browse files
authored
Merge pull request #2570 from subzeroid/fix-account-phone-confirmation-1625
Add phone confirmation code helper
2 parents 08cbc1e + c55a1ad commit 1ce9353

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

docs/usage-guide/account.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Viewing and managing your profile
2121
| change_password(old_password: str, new_password: str) | bool | Change account password |
2222
| send_confirm_email(email: str) | dict | Send a confirmation code to a new email address |
2323
| confirm_email(email: str, code: str) | dict | Confirm a new email address with the received code |
24+
| send_confirm_phone_number(phone_number: str) | dict | Send a confirmation code to a new phone number |
25+
| confirm_phone_number(phone_number: str, code: str, has_sms_consent: bool = False) | dict | Confirm a new phone number with the received SMS code |
2426

2527
Example:
2628

@@ -82,6 +84,9 @@ UserShort(pk=1903424587, username='example', ...)
8284
'robocall_after_max_sms': True},
8385
'status': 'ok'
8486
}
87+
88+
>>> cl.confirm_phone_number("+5599999999", "123456")
89+
{'status': 'ok'}
8590
```
8691

8792
Notes:

instagrapi/mixins/account.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,29 @@ def send_confirm_phone_number(self, phone_number: str) -> dict:
437437
}
438438
),
439439
)
440+
441+
def confirm_phone_number(self, phone_number: str, code: str, has_sms_consent: bool = False) -> dict:
442+
"""
443+
Confirm new phone number by SMS code
444+
445+
Parameters
446+
----------
447+
phone_number: str
448+
Phone number
449+
code: str
450+
Confirmation code
451+
has_sms_consent: bool, default False
452+
Whether to include Instagram's SMS consent flag
453+
454+
Returns
455+
-------
456+
dict
457+
Jsonified response from Instagram
458+
"""
459+
data = {"phone_number": phone_number, "verification_code": code}
460+
if has_sms_consent:
461+
data["has_sms_consent"] = "true"
462+
return self.private_request(
463+
"accounts/verify_sms_code/",
464+
self.with_extra_data(data),
465+
)

tests/regression/test_account.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,28 @@ def test_confirm_email_posts_verify_email_code_payload(self):
130130
"code": "123456",
131131
},
132132
)
133+
134+
def test_confirm_phone_number_posts_verify_sms_code_payload(self):
135+
client = Client()
136+
client.phone_id = "phone-id"
137+
client.authorization_data = {"ds_user_id": "123"}
138+
client.uuid = "uuid"
139+
client.android_device_id = "android-id"
140+
141+
with mock.patch.object(client, "private_request", return_value={"status": "ok"}) as private_request:
142+
result = client.confirm_phone_number("+15551234567", "123456", has_sms_consent=True)
143+
144+
self.assertEqual(result, {"status": "ok"})
145+
private_request.assert_called_once_with(
146+
"accounts/verify_sms_code/",
147+
{
148+
"_uuid": "uuid",
149+
"device_id": "android-id",
150+
"phone_id": "phone-id",
151+
"_uid": "123",
152+
"guid": "uuid",
153+
"phone_number": "+15551234567",
154+
"verification_code": "123456",
155+
"has_sms_consent": "true",
156+
},
157+
)

0 commit comments

Comments
 (0)