diff --git a/backend/endpoints/spotify_api.py b/backend/endpoints/spotify_api.py index b70f05a..a6af4cd 100644 --- a/backend/endpoints/spotify_api.py +++ b/backend/endpoints/spotify_api.py @@ -1,8 +1,9 @@ -from fastapi import APIRouter, Request, Depends -from fastapi.responses import RedirectResponse -import requests import os + +import requests from auth.session import SessionData, SessionManager +from fastapi import APIRouter +from fastapi.responses import RedirectResponse music_router = APIRouter(prefix="/music") @@ -13,6 +14,7 @@ SPOTIFY_AUTH_URL = "https://accounts.spotify.com/authorize" SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token" SPOTIFY_PLAY_URL = "https://api.spotify.com/v1/" + # Step 1: Redirect user to Spotify login @music_router.get("/login") def login(): @@ -25,6 +27,7 @@ def login(): ) return RedirectResponse(url) + # Step 2: Callback to get access token @music_router.get("/callback") def callback(code: str): @@ -46,6 +49,7 @@ def callback(code: str): return {} + @music_router.get("/search") def search(query: str): try: @@ -60,20 +64,27 @@ def search(query: str): except Exception as e: print(e) + @music_router.get("/play") def play(song_id: str): try: url = SPOTIFY_PLAY_URL + "me/player/play" - body = { "uris": [f"spotify:track:{song_id}"] } + body = { + "uris": [f"spotify:track:{song_id}"], + "position_ms": 0 + } header = { "Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens, "Content-Type": "application/json" } - response = requests.put(url, data=body, headers=header) + print(body) + response = requests.put(url,json=body, headers=header) return response.json() except Exception as e: print(e) + + @music_router.get("/current-song") def current_song(): try: