[GH-ISSUE #100] Cookie key change (SAPISID -> __Secure-3PAPISID) #73

Closed
opened 2026-02-27 22:07:53 +03:00 by kerem · 3 comments
Owner

Originally created by @xlorepdarkhelm on GitHub (Nov 7, 2020).
Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/100

When running ytmusicapi for the first time with the credentials I have generated following the YTMusic.setup() method, I was getting an error:


KeyError Traceback (most recent call last)
in
----> 1 ytmusic = ytmusicapi.YTMusic('headers_auth.json')

~/.pyenv/versions/3.9.0/envs/ytm-playlist/lib/python3.9/site-packages/ytmusicapi/ytmusic.py in init(self, auth, user, proxies, language)
94 # verify authentication credentials work
95 if auth:
---> 96 self.sapisid = sapisid_from_cookie(self.headers['Cookie'])
97 response = self._send_request('guide', {})
98 if 'error' in response:

~/.pyenv/versions/3.9.0/envs/ytm-playlist/lib/python3.9/site-packages/ytmusicapi/helpers.py in sapisid_from_cookie(raw_cookie)
41
42 # SAPISID Hash reverse engineered by
---> 43 # https://stackoverflow.com/a/32065323/5726546
44 def get_authorization(auth):
45 sha_1 = sha1()

KeyError: 'SAPISID'

Following this, I found that the line in error needs to be changed from:

return cookie["SAPISID"].value

to:

return cookie["__Secure-3PAPISID"].value

in ytmusicapi/helpers.py

This made the code work.

Originally created by @xlorepdarkhelm on GitHub (Nov 7, 2020). Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/100 When running ytmusicapi for the first time with the credentials I have generated following the YTMusic.setup() method, I was getting an error: > --------------------------------------------------------------------------- > KeyError Traceback (most recent call last) > <ipython-input-20-c5134c78bad4> in <module> > ----> 1 ytmusic = ytmusicapi.YTMusic('headers_auth.json') > > ~/.pyenv/versions/3.9.0/envs/ytm-playlist/lib/python3.9/site-packages/ytmusicapi/ytmusic.py in __init__(self, auth, user, proxies, language) > 94 # verify authentication credentials work > 95 if auth: > ---> 96 self.sapisid = sapisid_from_cookie(self.headers['Cookie']) > 97 response = self._send_request('guide', {}) > 98 if 'error' in response: > > ~/.pyenv/versions/3.9.0/envs/ytm-playlist/lib/python3.9/site-packages/ytmusicapi/helpers.py in sapisid_from_cookie(raw_cookie) > 41 > 42 # SAPISID Hash reverse engineered by > ---> 43 # https://stackoverflow.com/a/32065323/5726546 > 44 def get_authorization(auth): > 45 sha_1 = sha1() > > KeyError: 'SAPISID' Following this, I found that the line in error needs to be changed from: `return cookie["SAPISID"].value` to: `return cookie["__Secure-3PAPISID"].value` in ytmusicapi/helpers.py This made the code work.
kerem 2026-02-27 22:07:53 +03:00
  • closed this issue
  • added the
    yt-update
    label
Author
Owner

@sigma67 commented on GitHub (Nov 8, 2020):

I can't reproduce this, not even with a fresh session. I always have SAPISID in my cookies.

  • What POST request did you use to copy the headers?
  • Were you logged in at the time?
  • What browser did you use?
<!-- gh-comment-id:723594932 --> @sigma67 commented on GitHub (Nov 8, 2020): I can't reproduce this, not even with a fresh session. I always have SAPISID in my cookies. - What POST request did you use to copy the headers? - Were you logged in at the time? - What browser did you use?
Author
Owner

@arusahni commented on GitHub (Nov 8, 2020):

I encountered this issue yesterday, as well. The workaround in the OP fixed it for me.

  • I used the browse POST request
  • I was logged in at the time.
  • I was using Chrome.
<!-- gh-comment-id:723642373 --> @arusahni commented on GitHub (Nov 8, 2020): I encountered this issue yesterday, as well. The workaround in the OP fixed it for me. * I used the `browse` POST request * I was logged in at the time. * I was using Chrome.
Author
Owner

@sigma67 commented on GitHub (Nov 9, 2020):

I think the 3PAPISID is identical to the SAPISID in all cases for me, so nothing bad should come from implementing this. Unless there are some edge cases we are not aware of.

<!-- gh-comment-id:723886448 --> @sigma67 commented on GitHub (Nov 9, 2020): I think the 3PAPISID is identical to the SAPISID in all cases for me, so nothing bad should come from implementing this. Unless there are some edge cases we are not aware of.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ytmusicapi#73
No description provided.