我正在尝试在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'"
此_setUser方法以前在以前的版本中是公开的。 API的开发人员似乎建议你为每个调用使用login()函数,它会检查是否需要完成新的完整登录,如果不需要这样做,将调用_setUser。
在过去,为每个请求执行login()非常慢,但现在使用较新版本的私有API似乎要好得多。
要登录,您可以使用以下代码段:
$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();
_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);
}