-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpotifyAuthApp.py
60 lines (45 loc) · 1.84 KB
/
SpotifyAuthApp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from spotify_auth import SpotifyAuthenticator
from SyncSpotify_Prime import SyncSpotify_Prime
import sys
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication
SPOTIPY_CLIENT_ID = "your_client_id"
SPOTIPY_CLIENT_SECRET = "your_client_secret"
SPOTIPY_REDIRECT_URI = "http://localhost:62908/callback"
class OAuthHandler(QWidget):
def __init__(self, authenticator):
super().__init__()
self.authenticator = authenticator
self.webview = QWebEngineView(self)
self.webview.urlChanged.connect(self.handle_url_change)
layout = QVBoxLayout()
layout.addWidget(self.webview)
self.setLayout(layout)
# Get the authorization URL
auth_url = self.authenticator.get_authorization_url()
self.webview.setUrl(QUrl(auth_url))
def handle_url_change(self, qurl):
# Extract the redirected URL
redirect_url = qurl.toString()
# Check if the URL contains the expected redirect URI
if SPOTIPY_REDIRECT_URI in redirect_url:
# Get the access token
token_info = self.authenticator.get_access_token(redirect_url)
# Close the OAuthHandler window
self.close()
# Start the main application
app = QApplication(sys.argv)
window = SyncSpotify_Prime(token_info["access_token"])
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
app = QApplication(sys.argv)
# Initialize the SpotifyAuthenticator
spotify_authenticator = SpotifyAuthenticator(
SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, SPOTIPY_REDIRECT_URI
)
# Create and run the OAuthHandler window
oauth_handler = OAuthHandler(spotify_authenticator)
oauth_handler.show()
sys.exit(app.exec_())