首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Instagram私有InstagramAPI\Instagram::_setUser()错误

Instagram私有InstagramAPI\Instagram::_setUser()错误
EN

Stack Overflow用户
提问于 2018-06-04 06:21:42
回答 3查看 2.4K关注 0票数 0

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

代码语言:javascript
复制
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);
    }
}

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

代码语言:javascript
复制
"Call to protected method InstagramAPI\Instagram::_setUser() from context 'App\Http\Controllers\InstagramController'"
EN

回答 3

Stack Overflow用户

发布于 2018-10-28 13:00:54

在以前的版本中,此_setUser方法过去是公共的。API的开发人员似乎建议您在每次调用时都使用login()函数,它将检查是否需要进行新的完全登录,如果不需要,则将调用_setUser。

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

票数 1
EN

Stack Overflow用户

发布于 2018-06-04 14:34:41

对于登录,您可以使用以下代码片段:

代码语言:javascript
复制
$username = 'password';
$password =  'username';
$instagram = new Instagram(false, true, [
        'storage'    => 'mysql',
        'dbhost'     => 'localhost',
        'dbname'     => 'sessions',
        'dbusername' => 'root',
        'dbpassword' => '',
    ]);
$instagram->login($username, $password);

对于access用户id,您可以这样做:

代码语言:javascript
复制
$instagram->people->getUserIdForName($username);

在您成功登录后,尝试使用此方法访问当前用户:

代码语言:javascript
复制
$instagram->account->getCurrentUser()->getUser();
票数 0
EN

Stack Overflow用户

发布于 2019-02-22 20:05:40

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

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50671499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档