mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-27 00:25:54 +03:00
[GH-ISSUE #1125] Backend terminal shows: " Enter the URL you were redirected to: " #668
Labels
No labels
api-bug
bug
dependencies
documentation
duplicate
enhancement
external-ide
headless-mode
implicit-grant-flow
invalid
missing-endpoint
pr-welcome
private-api
pull-request
question
spotipy3
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/spotipy#668
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @wassay13 on GitHub (May 30, 2024).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1125
So I have build backend and frontend sapreatly and test them on localhost in which everything works perfectly fine, and because I'm using FastAPI for backend so I just build CacheHandler for it too:
But as soon as I deployed both on DigitalOcean VM I fall into lot of errors. (please look at return statement in root endpoint)
Till yet I've conclude two major scenarios: first that I cannot let user to authorize from my backend IP directly because of CORS. And second is that if I send auth_url and let frontend open it in new window then in my backend I'll get interactive terminal to enter url manually on behalf of them (what on earth?) also all the endpoints will get freeze till I don't stop server or put anything else.
Last Option:
Now I dig into Spotipy library to find out why I'm getting interactive terminal and find this:
So I thought why not just give the code manually and get rid of "or self.get_auth_response()" right? NO!
But I tried:
This approach solve " Enter the URL you were redirected to: " issue but who knows what coming..
Then from my frontend I call another endpoint:
Then the endpoint I call:
Now till getting back access token everything seems working perfectly fine but when I call "store_user_data(db, user, access_token, sp)" this function, my code gets broke, ss attached below :(


This is the problematic function which works perfectly fine on localhost but since I changed code to give it manually I don't even know what's happening under the hood:
After digging more I get to know I broke hell lot of things (ig), and need to rebuild and adjust this library for my use case which I cannot as I'm still relatively new into coding and don't understand much but it'll be highly appreciated if someone guide me what to do because this is my first project which I started building on my own and not clone from some youtube video. And there is high chances that I feel I'm missing something very basic which cause me this issue (coding solely with help of AI made me feel like this).
PS: And yeah I'm not giving the access token in "response = sp.current_user_followed_artists(limit=50, after=after)" because I don't understand where exactly it'll need to go, maybe in "self._get"?
@dieser-niko commented on GitHub (May 30, 2024):
It looks like you're on the right track.
The CORS has nothing to do with Spotify and has to be handled by FastAPI instead. You can read more about this here: https://fastapi.tiangolo.com/tutorial/cors/
Then the authentication process. I'd like you to take a look at this app.py example which shows how to get the authorise URL and how a callback from Spotify can be handled. This is a pretty good example, even though it is implemented in Flask. But there's not much difference to FastAPI in this case.
Admittedly, it's a bit tight, because the login process and the callback are implemented in the same function (and therefore the same endpoint).
But it's still possible to understand it. I'll explain it to you.
The function is divided into four parts.
First, the
cache_handlerandauth_managerare created. They are not passed tospotipy.Spotifyyet, and this is important, mainly because of the auth manager, as we don't want to trigger the internal authentication process.As for the
FlaskSessionCacheHandler, I'm not sure if there is something similar for FastAPI. I've seen that you've created your ownFastAPISessionCacheHandler, and honestly, it's probably fine to start with. You just have to remember that the user can easily extract the token. They shouldn't be able to do much damage as the token is tied to their account anyway.Then, in the second part, it checks if the incoming request is from a callback function by checking if "code" is one of the arguments used. The value of this argument is then checked for authenticity. If all is well, the user is logged in and the page is refreshed by redirecting to the same page.
The third part is to check if the user isn't logged in. If that's the case, then an authorize url is generated and returned to the user as a link. Yes, I know the order of the parts is a bit confusing, but bear with me. There are also comments here and in part 2 to indicate that this step is the first to be run.
And then the last part, where we finally get our
spotipy.Spotifyobject by passing ourauth_managerto it. Since we should already be logged in when we reach this part, there should be no prompt asking us to enter a link.I'd recommend running this code on your machine, but be sure to read the comment at the top of the file. It contains some useful information.
I hope I was able to help you.
@wassay13 commented on GitHub (May 31, 2024):
Hi Dieser, I tried the the exact same approach you suggest but its not working, heres why:
in
get_access_tokenwe haveSo when user hit
'/'endpoint to my backend (which is running on different server from frontend) for very first time, this function gets called:self.get_auth_response()and the nature of this function is to open new window in computer (server) orr interactive terminal by following the flow it calls:_get_auth_response_interactivein which it actually execute the logic.@dieser-niko commented on GitHub (Jun 1, 2024):
My name is Niko actually, but that's all right.
I don't think that the relevant part is included in your snippet, so that's why I straight up built a little example with FastAPI. You said that you're using it as a Backend, so the auth url will be returned as a simple JSON and not as a
307 Temporary Redirector similar. Check it out: https://github.com/dieser-niko/spotipy-fastapi-oauth@wassay13 commented on GitHub (Jun 3, 2024):
Hey hey hey, ty Niko, it works. Although I've tried it manually which also working perfectly fine but I'll update my code with this approach so I don't fall under unexpected error in future.
@dieser-niko commented on GitHub (Jun 3, 2024):
Glad to hear that. If you want to, you can change to an authorization header instead of cookies. Probably the only thing I regret about my code :D