【问题标题】:Instagram private InstagramAPI\Instagram::_setUser() errorInstagram 私人 InstagramAPI\Instagram::_setUser() 错误
【发布时间】:2018-06-03 22:21:42
【问题描述】:

我正在尝试在 laravel 上实现 https://github.com/mgp25/Instagram-API,在 Instagram 成功登录后,我必须使用 _setUser 在登录后使用现有数据,例如:

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);
    }
}

在这段代码中我得到这个错误:

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

【问题讨论】:

    标签: php laravel instagram instagramapi-mgp25


    【解决方案1】:

    这个 _setUser 方法曾经在以前的版本中是公开的。 API 的开发人员似乎建议您在每次调用时使用 login() 函数,它会检查是否需要完成新的完整登录,如果不需要完成,将调用 _setUser。

    过去为每个请求执行 login() 非常慢,但现在使用更新版本的私有 API 似乎要好得多。

    【讨论】:

      【解决方案2】:

      登录时可以使用这个sn-p:

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

      对于访问用户 ID,您可以这样做:

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

      成功登录后,试试这个访问当前用户:

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

      【讨论】:

      • 嗨 Hosein,你使用这个库吗,我登录没有任何问题,我得到了成功的结果,但是登录后我们不需要重新登录,我们必须使用 _setUser使用当前会话,到目前为止这个方法是protected 我尝试制作简单的类并使用它,但我也得到错误
      • @DolDurma 我多次使用这个包,当你第一次将会话信息保存在数据库中时,它不会为以后的调用登录(它会重复使用会话信息直到过期),并且您不再需要 _setUser。
      【解决方案3】:

      _setUser 函数是私有的,您可以将私有函数编辑为公共函数,然后只有您可以使用该函数,即使您不更改它也会自动采用 _setUser 方法,因为文件夹中的会话存储处于活动状态。您可以在下面查看

      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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多