我正在尝试在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'"发布于 2018-10-28 13:00:54
在以前的版本中,此_setUser方法过去是公共的。API的开发人员似乎建议您在每次调用时都使用login()函数,它将检查是否需要进行新的完全登录,如果不需要,则将调用_setUser。
在过去,为每个请求执行login()非常慢,但现在使用更新版本的私有API似乎要好得多。
发布于 2018-06-04 14:34:41
对于登录,您可以使用以下代码片段:
$username = 'password';
$password = 'username';
$instagram = new Instagram(false, true, [
'storage' => 'mysql',
'dbhost' => 'localhost',
'dbname' => 'sessions',
'dbusername' => 'root',
'dbpassword' => '',
]);
$instagram->login($username, $password);对于access用户id,您可以这样做:
$instagram->people->getUserIdForName($username);在您成功登录后,尝试使用此方法访问当前用户:
$instagram->account->getCurrentUser()->getUser();发布于 2019-02-22 20:05:40
_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);
}https://stackoverflow.com/questions/50671499
复制相似问题