Skip to content

Commit

Permalink
Fix login with cookie error (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsmle committed Aug 11, 2022
1 parent c2e5d62 commit e72571c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
36 changes: 31 additions & 5 deletions examples/login-with-cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,42 @@
use Instagram\Utils\CacheHelper;
use Instagram\Exception\{InstagramException, InstagramAuthException};
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use GuzzleHttp\Cookie\{SetCookie, CookieJar};

require realpath(dirname(__FILE__)) . '/../vendor/autoload.php';
$credentials = include_once realpath(dirname(__FILE__)) . '/credentials.php';

$cachePool = new FilesystemAdapter('Instagram', 0, __DIR__ . '/../cache');

// Make sure you are logged in with the login() method before using this example
// See examples in https://github.com/pgrimaud/instagram-user-feed/blob/5b2358f9918b84c11b7d193f7f3205df87b35793/examples/profile.php#L17-L18
$sessionData = $cachePool->getItem(Session::SESSION_KEY . '.' . CacheHelper::sanitizeUsername($credentials->getLogin()));
$cookies = $sessionData->get();
/**
* Make sure you are logged in with the login() method before using example "1. Get cookies from file"
* See examples in https://github.com/pgrimaud/instagram-user-feed/blob/5b2358f9918b84c11b7d193f7f3205df87b35793/examples/profile.php#L17-L18
*/

/** 1. Get cookies from file */
$sessionId = $cachePool->getItem(Session::SESSION_KEY . '.' . CacheHelper::sanitizeUsername($credentials->getLogin()))
->get()
->getCookieByName('sessionId');

// Generate CookieJar from instagram cookie 'sessionid'
$cookieJar = new CookieJar(false, [$sessionId]);

/** 2. Insert cookies manually
$sessionId = new SetCookie([
"Name" => "sessionid",
"Value" => "YOUR_INSTAGRAM_SESSIONID",
"Domain" => ".instagram.com",
"Path" => "/",
"Expires" => "YOUR_INSTAGRAM_SESSIONID_EXPIRES",
"Max-Age" => "31536000",
"Secure" => true,
"Discard" => false,
"HttpOnly" => true,
]);
// Generate CookieJar from instagram cookie 'sessionid'
$cookieJar = new CookieJar(false, [$sessionId]);
*/

try {
$api = new Api();
Expand All @@ -25,7 +51,7 @@
$api->setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.57 Safari/537.36');
$api->setLanguage('id-ID');

$api->loginWithCookies($cookies);
$api->loginWithCookies($cookieJar);

$profile = $api->getProfile('robertdowneyjr');

Expand Down
6 changes: 5 additions & 1 deletion src/Instagram/Auth/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ public function withCookies(array $session): CookieJar
preg_match('/<script type="text\/javascript">window\._sharedData\s?=(.+);<\/script>/', $html, $matches);

if (isset($matches[1])) {
throw new InstagramAuthException('Please login with instagram credentials.');
$data = json_decode($matches[1]);

if (!isset($data->config->viewer) && !isset($data->config->viewerId)) {
throw new InstagramAuthException('Please login with instagram credentials.');
}
}

return $cookies;
Expand Down

0 comments on commit e72571c

Please sign in to comment.