Skip to content

Commit

Permalink
can't connect [#252] Switched to a new authorization mechanism (from …
Browse files Browse the repository at this point in the history
…cookies to Authorization header)
  • Loading branch information
adw0rd committed Jul 8, 2021
1 parent 5b939d9 commit 180d079
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
15 changes: 14 additions & 1 deletion instagrapi/mixins/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ def get_reels_tray_feed(self, reason: str = "pull_to_refresh") -> Dict:
class LoginMixin(PreLoginFlowMixin, PostLoginFlowMixin):
username = None
password = None
authorization = ''
authorization_data = {} # decoded authorization header
last_login = None
relogin_attempt = 0
device_settings = {}
Expand Down Expand Up @@ -359,6 +361,12 @@ def login(self, username: str, password: str, relogin: bool = False, verificatio
}
try:
logged = self.private_request("accounts/login/", data, login=True)
# Example: Bearer IGT:2:eaW9u.....aWQiOiI0NzM5=
self.authorization = self.last_response.headers.get('ig-set-authorization')
try:
self.authorization_data = json.loads(base64.b64decode(self.authorization.rsplit(':', 1)[-1]))
except Exception as e:
self.logger.exception(e)
except TwoFactorRequired as e:
if not verification_code.strip():
raise TwoFactorRequired(f'{e} (you did not provide verification_code for login method)')
Expand Down Expand Up @@ -428,7 +436,10 @@ def cookie_dict(self) -> dict:

@property
def sessionid(self) -> str:
return self.cookie_dict.get("sessionid")
sessionid = self.cookie_dict.get("sessionid")
if not sessionid and self.authorization_data:
sessionid = self.authorization_data.get('sessionid')
return sessionid

@property
def token(self) -> str:
Expand All @@ -441,6 +452,8 @@ def rank_token(self) -> str:
@property
def user_id(self) -> int:
user_id = self.cookie_dict.get("ds_user_id")
if not user_id and self.authorization_data:
user_id = self.authorization_data.get('ds_user_id')
if user_id:
return int(user_id)
return None
Expand Down
5 changes: 5 additions & 0 deletions instagrapi/mixins/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ def private_request(
headers=None,
extra_sig=None,
):
if self.authorization:
if not headers:
headers = {}
if 'authorization' not in headers:
headers.update({'Authorization': self.authorization})
kwargs = dict(
data=data,
params=params,
Expand Down
5 changes: 3 additions & 2 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
flake8==3.9.2
Pillow==8.3.0
ipython==7.16.1
Pillow==8.3.1
ipython==7.25.0
pudb==2021.1
isort==5.9.1
bandit==1.7.0
mike==1.0.1
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

setup(
name='instagrapi',
version='1.8.1',
version='1.8.2',
author='Mikhail Andreev',
author_email='[email protected]',
license='MIT',
Expand Down

0 comments on commit 180d079

Please sign in to comment.