sigma67 / ytmusicapi

Unofficial API for YouTube Music
https://ytmusicapi.readthedocs.io
MIT License
1.59k stars 183 forks source link

Help to understand token refactoring #508

Closed KoljaWindeler closed 6 months ago

KoljaWindeler commented 6 months ago

Hi @sigma67, as you know I'm using your code to provide access to ytubemusic to users of homeassistant with this integration https://github.com/KoljaWindeler/ytube_music_player

I've added oAuth a while back and it worked absolutely awesome!! great work To be honest: I don't understand how to re-work it after the code was refactored :( can you give me a hand?

what i did was

from ytmusicapi.auth.oauth import YTMusicOAuth
... 
session = requests.Session()
self.oauth = YTMusicOAuth(session)
self.code = await self.hass.async_add_executor_job(self.oauth.get_code) 

... 

# let the user do the oAuth part and wait until the next step (validation)
try:
   self.token = await self.hass.async_add_executor_job(lambda: self.oauth.get_token_from_code(self.code["device_code"])) 
except:
   # inform user that the oauth wasnt completed

...

# if the token was ok, store it
await self.hass.async_add_executor_job(lambda: self.oauth.dump_token(self.token,self.data[CONF_HEADER_PATH])) 

I understood that

from ytmusicapi.auth.oauth import YTMusicOAuth
... 
session = requests.Session()
self.oauth = YTMusicOAuth(session)
self.code = await self.hass.async_add_executor_job(self.oauth.get_code) 

transitioned to

from ytmusicapi.auth.oauth import OAuthCredentials
session = requests.Session()
self.oauth = OAuthCredentials("","",session,"")
self.code = await self.hass.async_add_executor_job(self.oauth.get_code) 

correct?

And

self.token = await self.hass.async_add_executor_job(lambda: self.oauth.get_token_from_code(self.code["device_code"])) 

moved into

self.token = await self.hass.async_add_executor_job(lambda: self.oauth.token_from_code(self.code["device_code"])) 

but is not longer failing when the oauto process wasn't completed, correct? is there any way for me to check if it did?

How do I store the token? I've tried several things but it looks like my skills are limited here. Can you help me restore the original behavior (not by changing your code but by helping me with the correct calls)

KoljaWindeler commented 6 months ago

Ok, finally understood that I needed a refreshtoken .. working again.

sigma67 commented 6 months ago

Hi, sorry for the trouble with the breaking changes. The refactoring was not initiated by me but I take full responsibility.

I hope it all worked out and the class structure makes sense from your perspective. Feel free to suggest improvements

KoljaWindeler commented 6 months ago

No worries, it just took forever to understand it. Another thing that is odd: I had to redo all token. Is this expected? That would mean that all users would have to reconfigure all installations .. oh oh ;)

sigma67 commented 6 months ago

No, the format should stay the same. What issue did you face?

KoljaWindeler commented 6 months ago

So .. I usually have my dev PC and my productive PC in sync .. same token files .. that used to work fine.

So here I copy my real PC -> Dev PC

jkw@jkw-Lenovo-YOGA-3-Pro-1370 /home/projects/ytube_music_player (git)-[main] % sudo scp jkw@192.168.2.84:/tmp/ytube_header_carlo.json ../homeassistant/.storage/header_ytube_music_player_kolja.json 
jkw@192.168.2.84's password: 
ytube_header_carlo.json                                                                         100%  564   101.8KB/s   00:00    
jkw@jkw-Lenovo-YOGA-3-Pro-1370 /home/projects/ytube_music_player (git)-[main] % cat ../homeassistant/.storage/header_ytube_music_player_kolja.json
{
 "access_token": "ya29.a0AfB_byDxnqWV76DWxTcnXNxxxxxxxxxxxxxpB5zr8Uiq6yHOJLQZHxtlKW7O1rIdF8Hc0HLi3SV8Uo1uF7Dh9ixSYVoo9oTxXxIIsKYT8SWAq_tv2V_jpjFb-ovShWIcs7ByIS455R0oCIE2p6SLqMOZovdbQxthPmQaCgYKAUgSARISFQHGX2MiLB_8KobQkG57eZ8ddBdVgA0185",
 "expires_in": 84007,
 "refresh_token": "1//097C28v63-JcUCgYIARAAGAkSNwFxxxxxxxxxxxxD148jXoQGqATEuSnXhvuaCLzJ_i4A1C-ZZ6lfItBWffLv7vLr28n0MyQhA",
 "scope": "https://www.googleapis.com/auth/youtube",
 "token_type": "Bearer",
 "expires_at": 1704131622,
 "filepath": "/config/.storage/ytube_header_carlo.json"
}%                                        

This usually worked fine .. the token also still works fine on the productive setup but on my dev setup it results in an exception on the init

 api = await hass.async_add_executor_job(YTMusic,path)

after running another setup routine the file looks like this

jkw@jkw-Lenovo-YOGA-3-Pro-1370 /home/projects/ytube_music_player (git)-[main] % cat ../homeassistant/.storage/header_ytube_music_player_kolja.json
{
 "scope": "https://www.googleapis.com/auth/youtube",
 "token_type": "Bearer",
 "access_token": "ya29.a0AfB_byBCmkDbsCYf3jZAgAZecscFp-xxxxxxxxxxxOWIrjvgcQEH3aCCfc6jTnQkxkJ4lP2BOT8BSSmEo4j9P7IlMCt4PWwxxG0srMPfdZWRfXH782urOZLkFSSWrK7z3QaZJLJR_gqkbAG9m3JsyvSfSelchN8OXn-aVzEmaCgYKAdQSARASFQHGX2MivAFTJDmcN7ZjULFZeQALbg0183",
 "refresh_token": "1//09HQTBYoncLgrCgYIARAAGAkSNwF-L9IxxxxxxxxxxxxxxsDvjBkSvQNzO50ltqacI2uvERkd5to1n9jk0_Lgl1eFVroH5DgAw",
 "expires_at": 1704738451,
 "expires_in": 77479
}%

any idea? I'll try tomorrow to create a token file on 1.3.2 and load that using 1.4.2 both on my dev pc .. same machine for both but I'm almost sure that it will fail

KoljaWindeler commented 6 months ago

hmm never mind .. works .. strange .. maybe my testing killed something .. not sure .. will report if this happens to other users when all change to 1.4.2