Skip to content

Commit 22e6419

Browse files
author
werwolf2303
committed
Readme and implementation changes
Added optional auth url callback Removed user pass from readme Added oauth to readme
1 parent f210850 commit 22e6419

3 files changed

Lines changed: 46 additions & 6 deletions

File tree

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,43 @@ from librespot.zeroconf import ZeroconfServer
6262
zeroconf = ZeroconfServer.Builder().create()
6363
```
6464

65+
### Use OAuth for Login
66+
67+
#### Without auth url callback
68+
69+
```python
70+
from librespot.core import Session
71+
72+
# This will log an url in the terminal that you have to open
73+
74+
session = Session.Builder() \
75+
.oauth(None) \
76+
.create()
77+
```
78+
79+
#### With auth url callback
80+
81+
```python
82+
from librespot.core import Session
83+
84+
# This will pass the auth url to the method
85+
86+
def auth_url_callback(url):
87+
print(url)
88+
89+
session = Session.Builder() \
90+
.oauth(auth_url_callback) \
91+
.create()
92+
```
93+
6594
### Get Spotify's OAuth token
6695

6796
```python
6897
from librespot.core import Session
6998

7099

71100
session = Session.Builder() \
72-
.user_pass("Username", "Password") \
101+
.oauth(None) \
73102
.create()
74103

75104
access_token = session.tokens().get("playlist-read")
@@ -85,7 +114,7 @@ from librespot.metadata import TrackId
85114
from librespot.audio.decoders import AudioQuality, VorbisOnlyAudioQuality
86115

87116
session = Session.Builder() \
88-
.user_pass("Username", "Password") \
117+
.oauth(None) \
89118
.create()
90119

91120
track_id = TrackId.from_uri("spotify:track:xxxxxxxxxxxxxxxxxxxxxx")

librespot/core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,10 +1596,16 @@ def stored_file(self,
15961596
pass
15971597
return self
15981598

1599-
def oauth(self) -> Session.Builder:
1599+
def oauth(self, oauth_url_callback) -> Session.Builder:
1600+
"""
1601+
Login via OAuth
1602+
1603+
You can supply an oauth_url_callback method that takes a string and returns the OAuth URL.
1604+
When oauth_url_callback is None, this will block until logged in.
1605+
"""
16001606
if os.path.isfile(self.conf.stored_credentials_file):
16011607
return self.stored_file(None)
1602-
self.login_credentials = OAuth(MercuryRequests.keymaster_client_id, "http://127.0.0.1:5588/login").flow()
1608+
self.login_credentials = OAuth(MercuryRequests.keymaster_client_id, "http://127.0.0.1:5588/login", oauth_url_callback).flow()
16031609
return self
16041610

16051611
def user_pass(self, username: str, password: str) -> Session.Builder:

librespot/oauth.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ class OAuth:
2121
__code = ""
2222
__token = ""
2323
__server = None
24+
__oauth_url_callback = None
2425

25-
def __init__(self, client_id, redirect_url):
26+
def __init__(self, client_id, redirect_url, oauth_url_callback):
2627
self.__client_id = client_id
2728
self.__redirect_url = redirect_url
29+
self.__oauth_url_callback = oauth_url_callback
2830

2931
def __generate_generate_code_verifier(self):
3032
possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
@@ -39,7 +41,10 @@ def __generate_code_challenge(self, code_verifier):
3941

4042
def get_auth_url(self):
4143
self.__code_verifier = self.__generate_generate_code_verifier()
42-
return self.__spotify_auth % (self.__client_id, self.__redirect_url, self.__generate_code_challenge(self.__code_verifier), "+".join(self.__scopes))
44+
auth_url = self.__spotify_auth % (self.__client_id, self.__redirect_url, self.__generate_code_challenge(self.__code_verifier), "+".join(self.__scopes))
45+
if self.__oauth_url_callback:
46+
self.__oauth_url_callback(auth_url)
47+
return auth_url
4348

4449
def set_code(self, code):
4550
self.__code = code

0 commit comments

Comments
 (0)