[GH-ISSUE #378] setup oauth with function #297

Closed
opened 2026-02-27 22:09:03 +03:00 by kerem · 6 comments
Owner

Originally created by @MisileLab on GitHub (Apr 14, 2023).
Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/378

Is your feature request related to a problem? Please describe.
I want to setup oauth with function, but I can't it because setup_oauth function uses terminal.

Describe the solution you'd like
make a oauth function

Describe alternatives you've considered

Additional context

Originally created by @MisileLab on GitHub (Apr 14, 2023). Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/378 **Is your feature request related to a problem? Please describe.** I want to setup oauth with function, but I can't it because setup_oauth function uses terminal. **Describe the solution you'd like** make a oauth function **Describe alternatives you've considered** **Additional context**
kerem closed this issue 2026-02-27 22:09:04 +03:00
Author
Owner

@sigma67 commented on GitHub (Apr 14, 2023):

How would that work? Someone still needs to access the site, log in and confirm the authorization. It's not possible to do this in a fully automated fashion without user interaction, unless you use the browser auth.

<!-- gh-comment-id:1508987222 --> @sigma67 commented on GitHub (Apr 14, 2023): How would that work? Someone still needs to access the site, log in and confirm the authorization. It's not possible to do this in a fully automated fashion without user interaction, unless you use the `browser` auth.
Author
Owner

@sigma67 commented on GitHub (Apr 14, 2023):

I suppose it would be possible to move the input statement out of get_code, that would allow people to code their own oauth flows in say, Desktop apps.

You could then do:

from ytmusicapi.auth.oauth import YTMusicOAuth

session = requests.Session()
oauth = YTMusicOAuth(session)

code = oauth.get_code()

# tell the user to finish the flow at code['verification_url']

token = oauth.get_token_from_code(code["device_code"])

# store the token in oauth.json

Is that what you intended?

<!-- gh-comment-id:1509050453 --> @sigma67 commented on GitHub (Apr 14, 2023): I suppose it would be possible to move the input statement out of `get_code`, that would allow people to code their own oauth flows in say, Desktop apps. You could then do: ```python from ytmusicapi.auth.oauth import YTMusicOAuth session = requests.Session() oauth = YTMusicOAuth(session) code = oauth.get_code() # tell the user to finish the flow at code['verification_url'] token = oauth.get_token_from_code(code["device_code"]) # store the token in oauth.json ``` Is that what you intended?
Author
Owner

@MisileLab commented on GitHub (Apr 15, 2023):

Yes, thank you

<!-- gh-comment-id:1509469650 --> @MisileLab commented on GitHub (Apr 15, 2023): Yes, thank you
Author
Owner

@apastel commented on GitHub (Feb 11, 2024):

For anyone reading, in the latest version of ytmusicapi (1.5.2 as of today), it would use the OAuthCredentials class and the token_from_code() function.

from ytmusicapi.auth.oauth import OAuthCredentials

session = requests.Session()
oauth = OAuthCredentials(session=session)

code = oauth.get_code()

# tell the user to finish the flow at code['verification_url']

token = oauth.token_from_code(code["device_code"])

# store the token in oauth.json

However even though token_from_code is documented to return a RefreshableTokenDict, the token it's returning is missing the expires_at property, which makes it invalid and results in a 401 Unauthorized when trying to use it. It has expires_in and access_token but no expires_at. I'm trying to figure out how to make it a proper refresh token -- wondering if I'm missing something.

<!-- gh-comment-id:1937903182 --> @apastel commented on GitHub (Feb 11, 2024): For anyone reading, in the latest version of ytmusicapi (1.5.2 as of today), it would use the `OAuthCredentials` class and the `token_from_code()` function. ``` from ytmusicapi.auth.oauth import OAuthCredentials session = requests.Session() oauth = OAuthCredentials(session=session) code = oauth.get_code() # tell the user to finish the flow at code['verification_url'] token = oauth.token_from_code(code["device_code"]) # store the token in oauth.json ``` However even though `token_from_code` is documented to return a `RefreshableTokenDict`, the token it's returning is missing the `expires_at` property, which makes it invalid and results in a 401 Unauthorized when trying to use it. It has `expires_in` and `access_token` but no `expires_at`. I'm trying to figure out how to make it a proper refresh token -- wondering if I'm missing something.
Author
Owner

@sigma67 commented on GitHub (Feb 12, 2024):

@apastel what you want is a RefreshingToken:

github.com/sigma67/ytmusicapi@98e0bc749c/ytmusicapi/auth/oauth/token.py (L129-L131)

Feel free to suggest/submit non-breaking usability changes or documentation if you find time :)

<!-- gh-comment-id:1938200062 --> @sigma67 commented on GitHub (Feb 12, 2024): @apastel what you want is a `RefreshingToken`: https://github.com/sigma67/ytmusicapi/blob/98e0bc749cb15873b23be3b73894c8931371d339/ytmusicapi/auth/oauth/token.py#L129-L131 Feel free to suggest/submit non-breaking usability changes or documentation if you find time :)
Author
Owner

@apastel commented on GitHub (Feb 12, 2024):

@sigma67 Thanks, I figured it had something to do with that but wasn't sure.

My updated code looks like this and is working:

from ytmusicapi.auth.oauth import OAuthCredentials, RefreshingToken

session = requests.Session()
oauth = OAuthCredentials(session=session)

code = oauth.get_code()

# tell the user to finish the flow at code['verification_url']

raw_token = oauth.token_from_code(code["device_code"])
ref_token = RefreshingToken(credentials=oauth, **raw_token)

# store the token in oauth.json
ref_token.store_token(<path_to_store_token>)
<!-- gh-comment-id:1939118874 --> @apastel commented on GitHub (Feb 12, 2024): @sigma67 Thanks, I figured it had something to do with that but wasn't sure. My updated code looks like this and is working: ``` from ytmusicapi.auth.oauth import OAuthCredentials, RefreshingToken session = requests.Session() oauth = OAuthCredentials(session=session) code = oauth.get_code() # tell the user to finish the flow at code['verification_url'] raw_token = oauth.token_from_code(code["device_code"]) ref_token = RefreshingToken(credentials=oauth, **raw_token) # store the token in oauth.json ref_token.store_token(<path_to_store_token>) ```
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#297
No description provided.