Instagram private InstagramAPI\Instagram::_setUser() error

2.6k Views Asked by At

I'm trying to implementing https://github.com/mgp25/Instagram-API on laravel and after successful login in Instagram i must to use _setUser to use exiting data after login, for example:

public function check()
{
    $username = 'XXX';
    $password = 'XXX';
    $ig = new Instagram();
    try {
        $ig->_setUser($username, $password);
    } catch (\Exception $e) {
        echo 'Something went wrong: '.$e->getMessage()."\n";
        exit(0);
    }
}

in this code i get this error:

"Call to protected method InstagramAPI\Instagram::_setUser() from context 'App\Http\Controllers\InstagramController'"
3

There are 3 best solutions below

2
On

For login, you can use this snippet:

$username = 'password';
$password =  'username';
$instagram = new Instagram(false, true, [
        'storage'    => 'mysql',
        'dbhost'     => 'localhost',
        'dbname'     => 'sessions',
        'dbusername' => 'root',
        'dbpassword' => '',
    ]);
$instagram->login($username, $password);

and for access user id you can do this:

$instagram->people->getUserIdForName($username);

and after you successfully logged in, try this one for accessing current user:

$instagram->account->getCurrentUser()->getUser();
0
On

The _setUser Function is Private, You can Edit Private to Public function then only you can use the function, and even if you don't change it will take automatically _setUser Method because of session store in Folder is Active. You can check Below

protected function _login(
    $username,
    $password,
    $forceLogin = false,
    $appRefreshInterval = 1800)
{
    if (empty($username) || empty($password)) {
        throw new \InvalidArgumentException('You must provide a username and password to _login().');
    }
    // Switch the currently active user/pass if the details are different.
    if ($this->username !== $username || $this->password !== $password) {
        $this->_setUser($username, $password);
    }
    // Perform a full relogin if necessary.
    if (!$this->isMaybeLoggedIn || $forceLogin) {
        $this->_sendPreLoginFlow();
        try {
            $response = $this->request('accounts/login/')
                ->setNeedsAuth(false)
                ->addPost('phone_id', $this->phone_id)
                ->addPost('_csrftoken', $this->client->getToken())
                ->addPost('username', $this->username)
                ->addPost('adid', $this->advertising_id)
                ->addPost('guid', $this->uuid)
                ->addPost('device_id', $this->device_id)
                ->addPost('password', $this->password)
                ->addPost('login_attempt_count', 0)
                ->getResponse(new Response\LoginResponse());
        } catch (\InstagramAPI\Exception\InstagramException $e) {
            if ($e->hasResponse() && $e->getResponse()->isTwoFactorRequired()) {
                // Login failed because two-factor login is required.
                // Return server response to tell user they need 2-factor.
                return $e->getResponse();
            } else {
                // Login failed for some other reason... Re-throw error.
                throw $e;
            }
        }
        $this->_updateLoginState($response);
        $this->_sendLoginFlow(true, $appRefreshInterval);
        // Full (re-)login successfully completed. Return server response.
        return $response;
    }
    // Attempt to resume an existing session, or full re-login if necessary.
    // NOTE: The "return" here gives a LoginResponse in case of re-login.
    return $this->_sendLoginFlow(false, $appRefreshInterval);
}
0
On

This _setUser method used to be public in previous versions. The developers of the API seem to be recommending that you use login() function for every call and it will check if a new full login needs to be done, if it does not need to be done, _setUser will be called.

In the past doing a login() for every request was very slow, but seems to be much better now with newer versions of the private API.