Skip to content

Commit e72571c

Browse files
authored
Fix login with cookie error (#320)
1 parent c2e5d62 commit e72571c

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

examples/login-with-cookies.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,42 @@
77
use Instagram\Utils\CacheHelper;
88
use Instagram\Exception\{InstagramException, InstagramAuthException};
99
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
10+
use GuzzleHttp\Cookie\{SetCookie, CookieJar};
1011

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

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

16-
// Make sure you are logged in with the login() method before using this example
17-
// See examples in https://github.com/pgrimaud/instagram-user-feed/blob/5b2358f9918b84c11b7d193f7f3205df87b35793/examples/profile.php#L17-L18
18-
$sessionData = $cachePool->getItem(Session::SESSION_KEY . '.' . CacheHelper::sanitizeUsername($credentials->getLogin()));
19-
$cookies = $sessionData->get();
17+
/**
18+
* Make sure you are logged in with the login() method before using example "1. Get cookies from file"
19+
* See examples in https://github.com/pgrimaud/instagram-user-feed/blob/5b2358f9918b84c11b7d193f7f3205df87b35793/examples/profile.php#L17-L18
20+
*/
21+
22+
/** 1. Get cookies from file */
23+
$sessionId = $cachePool->getItem(Session::SESSION_KEY . '.' . CacheHelper::sanitizeUsername($credentials->getLogin()))
24+
->get()
25+
->getCookieByName('sessionId');
26+
27+
// Generate CookieJar from instagram cookie 'sessionid'
28+
$cookieJar = new CookieJar(false, [$sessionId]);
29+
30+
/** 2. Insert cookies manually
31+
$sessionId = new SetCookie([
32+
"Name" => "sessionid",
33+
"Value" => "YOUR_INSTAGRAM_SESSIONID",
34+
"Domain" => ".instagram.com",
35+
"Path" => "/",
36+
"Expires" => "YOUR_INSTAGRAM_SESSIONID_EXPIRES",
37+
"Max-Age" => "31536000",
38+
"Secure" => true,
39+
"Discard" => false,
40+
"HttpOnly" => true,
41+
]);
42+
43+
// Generate CookieJar from instagram cookie 'sessionid'
44+
$cookieJar = new CookieJar(false, [$sessionId]);
45+
*/
2046

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

28-
$api->loginWithCookies($cookies);
54+
$api->loginWithCookies($cookieJar);
2955

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

src/Instagram/Auth/Login.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ public function withCookies(array $session): CookieJar
148148
preg_match('/<script type="text\/javascript">window\._sharedData\s?=(.+);<\/script>/', $html, $matches);
149149

150150
if (isset($matches[1])) {
151-
throw new InstagramAuthException('Please login with instagram credentials.');
151+
$data = json_decode($matches[1]);
152+
153+
if (!isset($data->config->viewer) && !isset($data->config->viewerId)) {
154+
throw new InstagramAuthException('Please login with instagram credentials.');
155+
}
152156
}
153157

154158
return $cookies;

0 commit comments

Comments
 (0)