From c26dd56b72c799367e839c6fd9ca8b0195e77e1e Mon Sep 17 00:00:00 2001 From: Khuong Van Cong Date: Tue, 25 Aug 2020 13:40:04 +0700 Subject: [PATCH 1/5] add InstagramChallengeRecaptchaException --- .../InstagramChallengeRecaptchaException.php | 11 ++++++++ src/InstagramScraper/Instagram.php | 25 +++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 src/InstagramScraper/Exception/InstagramChallengeRecaptchaException.php diff --git a/src/InstagramScraper/Exception/InstagramChallengeRecaptchaException.php b/src/InstagramScraper/Exception/InstagramChallengeRecaptchaException.php new file mode 100644 index 00000000..f8351236 --- /dev/null +++ b/src/InstagramScraper/Exception/InstagramChallengeRecaptchaException.php @@ -0,0 +1,11 @@ +code === static::HTTP_BAD_REQUEST && isset($response->body->message) && $response->body->message == 'checkpoint_required' - && !empty($twoStepVerificator) ) { $response = $this->verifyTwoStep($response, $cookies, $twoStepVerificator); } elseif ((is_string($response->code) || is_numeric($response->code)) && is_string($response->body)) { @@ -1741,11 +1742,14 @@ public function isLoggedIn($session) /** * @param $response * @param $cookies - * @param TwoStepVerificationInterface $twoStepVerificator + * @param TwoStepVerificationInterface|null $twoStepVerificator + * * @return Response * @throws InstagramAuthException + * @throws InstagramChallengeRecaptchaException + * @throws InstagramChallengeSubmitPhoneNumberException */ - private function verifyTwoStep($response, $cookies, $twoStepVerificator) + private function verifyTwoStep($response, $cookies, TwoStepVerificationInterface $twoStepVerificator = null) { $new_cookies = $this->parseCookies($response->headers); $cookies = array_merge($cookies, $new_cookies); @@ -1762,6 +1766,17 @@ private function verifyTwoStep($response, $cookies, $twoStepVerificator) $url = Endpoints::BASE_URL . $response->body->checkpoint_url; $response = Request::get($url, $headers); + + if (preg_match('/"challengeType":"RecaptchaChallengeForm"/', $response->raw_body, $matches)) { + throw new InstagramChallengeRecaptchaException('Instagram asked to enter the captcha.', $response->code); + } + + if (! preg_match('/"input_name":"security_code"/', $response->raw_body, $matches)) { + throw new InstagramAuthException('Something went wrong when try two step verification. Please report issue.', $response->code); + } elseif (! $twoStepVerificator instanceof TwoStepVerificationInterface) { + throw new InstagramAuthException('$twoStepVerificator must be an instance of TwoStepVerificationInterface.', $response->code); + } + if (preg_match('/window._sharedData\s\=\s(.*?)\;<\/script>/', $response->raw_body, $matches)) { $data = json_decode($matches[1], true, 512, JSON_BIGINT_AS_STRING); if (!empty($data['entry_data']['Challenge'][0]['extraData']['content'][3]['fields'][0]['values'])) { @@ -1782,10 +1797,6 @@ private function verifyTwoStep($response, $cookies, $twoStepVerificator) } } - if (!preg_match('/"input_name":"security_code"/', $response->raw_body, $matches)) { - throw new InstagramAuthException('Something went wrong when try two step verification. Please report issue.', $response->code); - } - $security_code = $twoStepVerificator->getSecurityCode(); $post_data = [ From 5c9d61380eb3bc7593cd96498cdaf6ff971fca22 Mon Sep 17 00:00:00 2001 From: Khuong Van Cong Date: Tue, 25 Aug 2020 13:40:58 +0700 Subject: [PATCH 2/5] add InstagramChallengeSubmitPhoneNumberException --- .../InstagramChallengeSubmitPhoneNumberException.php | 11 +++++++++++ src/InstagramScraper/Instagram.php | 2 ++ 2 files changed, 13 insertions(+) create mode 100644 src/InstagramScraper/Exception/InstagramChallengeSubmitPhoneNumberException.php diff --git a/src/InstagramScraper/Exception/InstagramChallengeSubmitPhoneNumberException.php b/src/InstagramScraper/Exception/InstagramChallengeSubmitPhoneNumberException.php new file mode 100644 index 00000000..7c27b4e0 --- /dev/null +++ b/src/InstagramScraper/Exception/InstagramChallengeSubmitPhoneNumberException.php @@ -0,0 +1,11 @@ +raw_body, $matches)) { throw new InstagramChallengeRecaptchaException('Instagram asked to enter the captcha.', $response->code); + } elseif (preg_match('/"challengeType":"SubmitPhoneNumberForm"/', $response->raw_body, $matches)) { + throw new InstagramChallengeSubmitPhoneNumberException('Instagram asked to enter a phone number.', $response->code); } if (! preg_match('/"input_name":"security_code"/', $response->raw_body, $matches)) { From 3e6dda3c59f96b943076d6caf8e29836593e0cef Mon Sep 17 00:00:00 2001 From: Khuong Van Cong Date: Tue, 25 Aug 2020 15:51:23 +0700 Subject: [PATCH 3/5] support challengeType:SelectVerificationMethodForm --- src/InstagramScraper/Instagram.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index 64233e18..d9abded1 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -1773,7 +1773,9 @@ private function verifyTwoStep($response, $cookies, TwoStepVerificationInterface throw new InstagramChallengeSubmitPhoneNumberException('Instagram asked to enter a phone number.', $response->code); } - if (! preg_match('/"input_name":"security_code"/', $response->raw_body, $matches)) { + if (! preg_match('/"input_name":"security_code"/', $response->raw_body, $matches) + && ! preg_match('/"challengeType":"SelectVerificationMethodForm"/', $response->raw_body, $matches) + ) { throw new InstagramAuthException('Something went wrong when try two step verification. Please report issue.', $response->code); } elseif (! $twoStepVerificator instanceof TwoStepVerificationInterface) { throw new InstagramAuthException('$twoStepVerificator must be an instance of TwoStepVerificationInterface.', $response->code); From 03c5f0c80343d3697e2d3e623e7183c99969e16e Mon Sep 17 00:00:00 2001 From: Khuong Van Cong Date: Tue, 25 Aug 2020 16:14:23 +0700 Subject: [PATCH 4/5] correct --- src/InstagramScraper/Instagram.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index d9abded1..51c877b9 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -1773,11 +1773,8 @@ private function verifyTwoStep($response, $cookies, TwoStepVerificationInterface throw new InstagramChallengeSubmitPhoneNumberException('Instagram asked to enter a phone number.', $response->code); } - if (! preg_match('/"input_name":"security_code"/', $response->raw_body, $matches) - && ! preg_match('/"challengeType":"SelectVerificationMethodForm"/', $response->raw_body, $matches) - ) { - throw new InstagramAuthException('Something went wrong when try two step verification. Please report issue.', $response->code); - } elseif (! $twoStepVerificator instanceof TwoStepVerificationInterface) { + // for 2FA case + if (! $twoStepVerificator instanceof TwoStepVerificationInterface) { throw new InstagramAuthException('$twoStepVerificator must be an instance of TwoStepVerificationInterface.', $response->code); } @@ -1801,6 +1798,10 @@ private function verifyTwoStep($response, $cookies, TwoStepVerificationInterface } } + if (!preg_match('/"input_name":"security_code"/', $response->raw_body, $matches)) { + throw new InstagramAuthException('Something went wrong when try two step verification. Please report issue.', $response->code); + } + $security_code = $twoStepVerificator->getSecurityCode(); $post_data = [ From 4ec4bcd5d7c8c57ea7bc67de537f76d158174a41 Mon Sep 17 00:00:00 2001 From: Khuong Van Cong Date: Wed, 26 Aug 2020 11:31:01 +0700 Subject: [PATCH 5/5] correct phpdocs --- src/InstagramScraper/Instagram.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index 51c877b9..1428f77d 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -1625,9 +1625,11 @@ public function getStories($reel_ids = null) * $support_two_step_verification true works only in cli mode - just run login in cli mode - save cookie to file and use in any mode * * @return array - * @throws InstagramException - * * @throws InstagramAuthException + * @throws InstagramChallengeRecaptchaException + * @throws InstagramChallengeSubmitPhoneNumberException + * @throws InstagramException + * @throws \Psr\SimpleCache\InvalidArgumentException */ public function login($force = false, $twoStepVerificator = null) {