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 +1744,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 +1768,18 @@ 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); + } elseif (preg_match('/"challengeType":"SubmitPhoneNumberForm"/', $response->raw_body, $matches)) { + throw new InstagramChallengeSubmitPhoneNumberException('Instagram asked to enter a phone number.', $response->code); + } + + // for 2FA case + if (! $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'])) {