[GH-ISSUE #195] [REQUEST] Create session using user's token #34

Closed
opened 2026-02-27 08:11:32 +03:00 by kerem · 8 comments
Owner

Originally created by @staniel359 on GitHub (May 21, 2023).
Original GitHub issue: https://github.com/kokarare1212/librespot-python/issues/195

Is your feature request related to a problem? Please describe.

I'm running your library on a server for multiple users.
Right now I'm using my own Spotify credentials for all users.
When a user decides to connect their account, instead of storing their credentials I'd like to store their access token (for security reasons) and use it to create each user's session.

Describe the solution you'd like

Session.Builder().token(<USER_TOKEN>)

Originally created by @staniel359 on GitHub (May 21, 2023). Original GitHub issue: https://github.com/kokarare1212/librespot-python/issues/195 **Is your feature request related to a problem? Please describe.** I'm running your library on a server for multiple users. Right now I'm using my own Spotify credentials for all users. When a user decides to connect their account, instead of storing their credentials I'd like to store their access token (for security reasons) and use it to create each user's session. **Describe the solution you'd like** `Session.Builder().token(<USER_TOKEN>)`
kerem 2026-02-27 08:11:32 +03:00
Author
Owner

@kokarare1212 commented on GitHub (May 21, 2023):

Perhaps this should work.

session = Session.Builder() \
    .user_pass("<email address>", "<password>") \
    .create()

stored_credentials = session.stored()
session2 = Session.Builder().stored(stored_credentials).create()
<!-- gh-comment-id:1556135670 --> @kokarare1212 commented on GitHub (May 21, 2023): Perhaps this should work. ```python session = Session.Builder() \ .user_pass("<email address>", "<password>") \ .create() stored_credentials = session.stored() session2 = Session.Builder().stored(stored_credentials).create() ```
Author
Owner

@staniel359 commented on GitHub (May 21, 2023):

@kokarare1212 But what if I don't want to use any users' emails or passwords?
Is it possible to login user via their token?

<!-- gh-comment-id:1556136705 --> @staniel359 commented on GitHub (May 21, 2023): @kokarare1212 But what if I don't want to use any users' emails or passwords? Is it possible to login user via their token?
Author
Owner

@kokarare1212 commented on GitHub (May 21, 2023):

It is necessary for the first login, but from the next time onward, such a thing can be done by obtaining something like a token in session.stored and passing it as an argument to Session.Builder.stored, as in the above code.

<!-- gh-comment-id:1556137206 --> @kokarare1212 commented on GitHub (May 21, 2023): It is necessary for the first login, but from the next time onward, such a thing can be done by obtaining something like a token in session.stored and passing it as an argument to Session.Builder.stored, as in the above code.
Author
Owner

@staniel359 commented on GitHub (May 21, 2023):

@kokarare1212 Okay, thanks.

<!-- gh-comment-id:1556137482 --> @staniel359 commented on GitHub (May 21, 2023): @kokarare1212 Okay, thanks.
Author
Owner

@prime-rue commented on GitHub (Jun 8, 2025):

It is necessary for the first login, but from the next time onward, such a thing can be done by obtaining something like a token in session.stored and passing it as an argument to Session.Builder.stored, as in the above code.

I can't get it to work. Could you please explain to me how I could use the following credentials to create a session?

{'access_token': 'BQAHHtai9r6xIHb2SPVYUCMG7XCoDbzhZGX0Qz-XXXXXXXXXXXXXXXXXX-uYj6g41R_PFQGMru5b60ZVkPiW83Dutw8VVRd35BAEJB0mRfBaeFU8aEhiDxF8Toy5gVI7eoaC_6Cn36So
-Ztx6me1SBMe_BMu6i_FBhUfJyTWTyjINX5whrTnwgqRcDlUu6ps8tl0iv_uwYA1FjRXjrnZmLXXXXXXXXXXXXXXXX',
'token_type': 'Bearer',
'expires_in': 3600,
'refresh_token': 'AQBLAs-XXXXXXXXXXXXXXXXXX-BMfRqEqZaAtZLzaRAmGny1jnseoOSOD_tDDB5GbwERZDYvqTMz26HMmDA32Rpw3UmEz3lhXk7z3NUAMjsxkr_yNLgTb273HrDdrxc3NwI',
'scope': 'user-library-read',
'expires_at': 1748377911}

I tried:

session = Session.Builder()

session.login_credentials = Authentication.LoginCredentials(
typ=Authentication.AUTHENTICATION_SPOTIFY_TOKEN,
username=SPOTIPY_CLIENT_USERNAME, # The username I specified for receiving the credentials.
auth_data=b'access_token'
)

session = session.create()

I obtained the credentials using spotipy.

<!-- gh-comment-id:2953826264 --> @prime-rue commented on GitHub (Jun 8, 2025): > It is necessary for the first login, but from the next time onward, such a thing can be done by obtaining something like a token in session.stored and passing it as an argument to Session.Builder.stored, as in the above code. I can't get it to work. Could you please explain to me how I could use the following credentials to create a session? {'access_token': 'BQAHHtai9r6xIHb2SPVYUCMG7XCoDbzhZGX0Qz-XXXXXXXXXXXXXXXXXX-uYj6g41R_PFQGMru5b60ZVkPiW83Dutw8VVRd35BAEJB0mRfBaeFU8aEhiDxF8Toy5gVI7eoaC_6Cn36So -Ztx6me1SBMe_BMu6i_FBhUfJyTWTyjINX5whrTnwgqRcDlUu6ps8tl0iv_uwYA1FjRXjrnZmLXXXXXXXXXXXXXXXX', 'token_type': 'Bearer', 'expires_in': 3600, 'refresh_token': 'AQBLAs-XXXXXXXXXXXXXXXXXX-BMfRqEqZaAtZLzaRAmGny1jnseoOSOD_tDDB5GbwERZDYvqTMz26HMmDA32Rpw3UmEz3lhXk7z3NUAMjsxkr_yNLgTb273HrDdrxc3NwI', 'scope': 'user-library-read', 'expires_at': 1748377911} I tried: session = Session.Builder() session.login_credentials = Authentication.LoginCredentials( typ=Authentication.AUTHENTICATION_SPOTIFY_TOKEN, username=SPOTIPY_CLIENT_USERNAME, # The username I specified for receiving the credentials. auth_data=b'access_token' ) session = session.create() I obtained the credentials using spotipy.
Author
Owner

@kokarare1212 commented on GitHub (Jun 8, 2025):

@prime-rue You probably cannot use Web API tokens.

<!-- gh-comment-id:2953838244 --> @kokarare1212 commented on GitHub (Jun 8, 2025): @prime-rue You probably cannot use Web API tokens.
Author
Owner

@prime-rue commented on GitHub (Jun 8, 2025):

@kokarare1212 You probably cannot use Web API tokens.

Okay, thank you for your quick reply. Are there any other methods beside zeroconf, which I can use to login individual users?

<!-- gh-comment-id:2953862795 --> @prime-rue commented on GitHub (Jun 8, 2025): > @kokarare1212 You probably cannot use Web API tokens. Okay, thank you for your quick reply. Are there any other methods beside zeroconf, which I can use to login individual users?
Author
Owner

@kokarare1212 commented on GitHub (Jun 8, 2025):

Currently, you can log in using Zeroconf or by obtaining an authentication file using another librespot, but the latter cannot be supported here.

<!-- gh-comment-id:2954081660 --> @kokarare1212 commented on GitHub (Jun 8, 2025): Currently, you can log in using Zeroconf or by obtaining an authentication file using another librespot, but the latter cannot be supported here.
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/librespot-python-kokarare1212#34
No description provided.