postaddictme / instagram-php-scraper

Get account information, photos, videos, stories and comments.
https://packagist.org/packages/raiym/instagram-php-scraper
MIT License
3.09k stars 800 forks source link

We Detected An Unusual Login Attempt #789

Closed tenet912 closed 3 years ago

tenet912 commented 3 years ago

i have this error on login

Fatal error: Uncaught InstagramScraper\Exception\InstagramAuthException: Something went wrong. Please report issue. in \src\InstagramScraper\Instagram.php:1685 Stack trace: #0 C:\xampp\htdocs\zhav\ajax.php(432): InstagramScraper\Instagram->login() #1 {main} thrown in \src\InstagramScraper\Instagram.php on line 1685

and after this when i login in instagram web i see the We Detected An Unusual Login Attempt page

popmatik commented 3 years ago

I was receiving this error too. I'm afraid. Any ideas would be massively appreciated!

jeybin commented 3 years ago

Hi all, I also got the same issue earlier, then I tried to keep the login response in the session an example using Laravel framework is mentioned below


    public static function connect($login=false){
                try{
        if($login){
            // checking, if the previous session exists returns value
            if(Session::has('instascrapper') && !empty(Session::get('instascrapper'))){
                return Session::get('instascrapper');
            }else{
                    sleep(3);// mimc user
                    // call the function login function and  save the response in session
                    $instagram = Instagram::withCredentials(self::USERNAME, self::PASSWORD, new Psr16Adapter('Files'));
                    $instagram->login();
                    $instagram->saveSession();
                   // saving session and returning 
                   Session::put('instascrapper',$instagram);
                    return $instagram;
            }
        }else{
            $instagram =  new Instagram();
            return $instagram;
        }
                }catch(Exception $exception){
                    return $exception;
                }
    }

    public function LikeMedia(Request $request){
            try {
               if($request->has('media')){
                     $media_id = $request->media;
                    // calling the above connect function 
                    $instagram = InstagramClient::connect(true);
                    $instagram->unlike($media_id);
                }else{
                    return $this->responseWithStatus(422,'Media id required');
                }
            } catch (\InstagramException $ex) {
                echo $ex->getMessage();
            }
    }

These functions are working for me perfectly By keeping the login response in the session we can avoid sending multiple requests for the actual login

VrasidasP commented 3 years ago

For me Instagram::withCredentials always throws the exception: "Something went wrong. Please report issue" (code 400), so I cannot get the login response to save it...

tenet912 commented 3 years ago

Hi all, I also got the same issue earlier, then I tried to keep the login response in the session an example using Laravel framework is mentioned below


   public static function connect($login=false){
               try{
       if($login){
           // checking, if the previous session exists returns value
           if(Session::has('instascrapper') && !empty(Session::get('instascrapper'))){
               return Session::get('instascrapper');
           }else{
                   sleep(3);// mimc user
                   // call the function login function and  save the response in session
                   $instagram = Instagram::withCredentials(self::USERNAME, self::PASSWORD, new Psr16Adapter('Files'));
                   $instagram->login();
                   $instagram->saveSession();
                  // saving session and returning 
                  Session::put('instascrapper',$instagram);
                   return $instagram;
           }
       }else{
           $instagram =  new Instagram();
           return $instagram;
       }
               }catch(Exception $exception){
                   return $exception;
               }
   }
    public function LikeMedia(Request $request){
            try {
               if($request->has('media')){
                     $media_id = $request->media;
                    // calling the above connect function 
                    $instagram = InstagramClient::connect(true);
                    $instagram->unlike($media_id);
                }else{
                    return $this->responseWithStatus(422,'Media id required');
                }
            } catch (\InstagramException $ex) {
                echo $ex->getMessage();
            }
    }

These functions are working for me perfectly By keeping the login response in the session we can avoid sending multiple requests for the actual login

please explain me on this codes . thanks

`use InstagramScraper\Instagram; use Phpfastcache\Helper\Psr16Adapter;

$user = ''; $password = "'';"; $instagram = \InstagramScraper\Instagram::withCredentials( $user, $password, new Psr16Adapter('Files')); $instagram->login();

// For getting information about account you don't need to auth:

$account = $instagram->getAccount('kevin');

// Available fields echo "Account info:\n"; echo "Id: {$account->getId()}\n"; echo "Username: {$account->getUsername()}\n"; echo "Full name: {$account->getFullName()}\n"; echo "Biography: {$account->getBiography()}\n"; echo "Profile picture url: {$account->getProfilePicUrl()}\n"; echo "External link: {$account->getExternalUrl()}\n"; echo "Number of published posts: {$account->getMediaCount()}\n"; echo "Number of followers: {$account->getFollowsCount()}\n"; echo "Number of follows: {$account->getFollowedByCount()}\n"; echo "Is private: {$account->isPrivate()}\n"; echo "Is verified: {$account->isVerified()}\n";`

how can i use your function in this codes ?

tenet912 commented 3 years ago

For me Instagram::withCredentials always throws the exception: "Something went wrong. Please report issue" (code 400), so I cannot get the login response to save it...

you have the same problem ?

jeybin commented 3 years ago

Hi @tenet912 ,

$user = ''; $password = "'';"; $instagram = \InstagramScraper\Instagram::withCredentials( $user, $password, new Psr16Adapter('Files')); $instagram->login(); $instagram->saveSession(); after this print and see what's in there in the $instagram variable, Save that value in the session, if you are developing a website or web application : $_SESSION["instagram"] = $instagram; you can call any where you want eg:

like this way you can skip withCredentials function calling repeatedly

if (isset($_SESSION['errors'])){ $instagram = $_SESSION["instagram"]; }else{ $instagram = \InstagramScraper\Instagram::withCredentials( $user, $password, new Psr16Adapter('Files')); $instagram->login(); $instagram->saveSession(); $_SESSION["instagram"] = $instagram; }

$account = $instagram->getAccount('kevin');

also if you don't want to call the getAccount function using credentials simply call

$instagram = new \InstagramScraper\Instagram(); // For getting information about account you don't need to auth: $account = $instagram->getAccount('kevin');

cleyversoncosta commented 3 years ago

I am getting the message “unusual login attempts” on first attempt to perform login.

Any suggestion on how to request the security code and submit it using this package??

APTEMOH commented 3 years ago

Save in file?

mmejia21 commented 3 years ago

Hi @tenet912 ,

$user = ''; $password = "'';"; $instagram = \InstagramScraper\Instagram::withCredentials( $user, $password, new Psr16Adapter('Files')); $instagram->login(); $instagram->saveSession(); after this print and see what's in there in the $instagram variable, Save that value in the session, if you are developing a website or web application : $_SESSION["instagram"] = $instagram; you can call any where you want eg:

like this way you can skip withCredentials function calling repeatedly

if (isset($_SESSION['errors'])){ $instagram = $_SESSION["instagram"]; }else{ $instagram = \InstagramScraper\Instagram::withCredentials( $user, $password, new Psr16Adapter('Files')); $instagram->login(); $instagram->saveSession(); $_SESSION["instagram"] = $instagram; }

$account = $instagram->getAccount('kevin');

also if you don't want to call the getAccount function using credentials simply call

$instagram = new \InstagramScraper\Instagram(); // For getting information about account you don't need to auth: $account = $instagram->getAccount('kevin');

Will this also save the session when running in terminal on MacOS? Trying to prevent a login each time I execute a function.

ricdijk commented 3 years ago

Might be different use case, but still wanted to share, might be useful.

If I understand the working correct, when you create the session with Psr16Adapter, the information is already stored on file and reused (you can find the cache in the directory './phpfastcache/xxx/Files/8c/0c').

The main steps of the Login() are:

  1. Read session from Psr16Adapter cache ('$session = static::$instanceCache->get($this->getCacheKey());')
  2. Check if session exists and is still loggedin \@Instagram ('if ($force || !$this->isLoggedIn($session))')
  3. if no $session or not loggedIn => login

So in my understanding the session is stored and should be reused without new login. The issue that I had with this method is that (at least in my case) the Psr16Adapter expired in 900 seconds after creation, which did not update on a new save. So I got a new login after each time after 900 seconds and a lot of login mails from Instagram. I made a change and use '$instagram->saveSession(86400);' to ensure the cache remains valid for a while and the expiration date is update upon each request.

Note: The actual checking in step 2 of the login takes a call on instagram, which does not seem necessary, so I skipped that in my application if the $session is available.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.