mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-26 16:15:51 +03:00
[GH-ISSUE #555] Overriding the auth-manager when I have obtained access_token and refresh_token outside of spotipy #335
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#335
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 @peey on GitHub (Aug 17, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/555
Is it possible for me to set
access_tokenandrefresh_tokento spotipy (or even justaccess_token) without spotipy having to worry about the authentication flow?I have taken care of the authentication flow in a different part of the app but still would like to be able to use spotipy as an API wrapper
@peey commented on GitHub (Aug 17, 2020):
I went through the code, and this is supported, but actually it isn't documented. Maybe we should add this to documentation as well.
A MWE:
After this, you can at least query for as long as the access token is valid. I don't know if that can also be automated (by passing
SpotifyOAuthobject as one of the params maybe?)@peey commented on GitHub (Aug 17, 2020):
Another way this can be done (with automated refreshing) is via a custom auth manager, I'd suggest adding this to the examples as well
@andyd0 commented on GitHub (Sep 9, 2020):
@peey did you have issues with
refresh_access_token? I just tried your code but eventuallyget_access_tokenfails because of the unexpectedasDictargument@andyd0 commented on GitHub (Sep 11, 2020):
I ended up getting the token using Spotify's web api tutorial and reduced your get_access_token method to just return that token. This worked for my purposes.
My guess is that the overriding of
get_access_tokenis not working correctly due to recent changes of addingas_dictas an argument. if i remember correctly, it fails at the client's_auth_headersi'm fine with refreshing manually when I need it but fyi for anyone else that sees this request and want to use it
@kwakubiney commented on GitHub (Aug 3, 2021):
@andyd0 @peey I tried to implement your solution but then I keep getting an error
http status: 403, code:-1 - https://api.spotify.com/v1/me/tracks?limit=40&offset=0: Insufficient client scope, reason: None. My guess is that thescopedoesn't get passed to theOAuth object. This is the code#auth manager to handle auth
@Peter-Schorn
@Peter-Schorn commented on GitHub (Aug 3, 2021):
It's impossible for me to determine the issue if you don't post the code that implements the authorization process. Why don't you just use the built in auth managers?
@peey Your
CustomAuthManageris completely pointless and should never be used. Just useSpotifyOAuthdirectly instead. It already automatically refreshes the access token when necessary for you.@peey commented on GitHub (Aug 3, 2021):
Sorry guys, I only used this library for a weekend project and that's when I posted the issue and the workaround. I haven't used this library since (haven't even run the code much), so I won't be able to help more here.
With the above disclaimer in place, @Peter-Schorn the most I can say is that I wanted to use an
access_tokenthat I obtained outside of spotipy (described more in first post of the issue), and I didn't find a way to do it at that time so I developed that code.@kwakubiney commented on GitHub (Aug 3, 2021):
@Peter-Schorn The thing is I already have a refresh token and access token in my database from
django-allauth. I wanted a workaround where I would just use both the refresh token and access token for API calls usingSpotipywithout going through theauthprocess of Spotipy.The
SpotifyOauthdefines the auth manager to be passed to aSpotifyobject which then makes the API calls. But how do I use theSpotifyobject if I already have these in my DB? I wanted to avoid using two access tokens, one for authentication to my site and one for API calls. Wanted to use just one for both.SpotifyOAuthseems to take care of refreshing the token perfectly but is there a way to pass my access token insteadThis is the extra code I did not write earlier
seems to be failing at
github.com/plamere/spotipy@b80bfa5c52/spotipy/client.py (L217)after
github.com/plamere/spotipy@b80bfa5c52/spotipy/client.py (L293)is called fromgithub.com/plamere/spotipy@b80bfa5c52/spotipy/client.py (L1232)@Peter-Schorn commented on GitHub (Aug 3, 2021):
The builtin authorization managers don't require you to go through the authorization process again if you've already done so and have the token info stored somewhere.
The solution to your problem is to create a custom class that inherits from
CacheHandler. Pass an instance of this class into the initializer for any of the auth managers (SpotifyOAuth,SpotifyPKCE,SpotifyClientCredentials). The cache handler is responsible for retrieving the authorization information and storing it whenever it changes. Use theCacheFileHandlerandMemoryCacheHandleras a guide.@andyd0 commented on GitHub (Aug 3, 2021):
Sorry @kweku-45 - This was a one and done thing for me so I no longer remember how I handled it then.
@kwakubiney commented on GitHub (Aug 4, 2021):
@Peter-Schorn does
token_infohave to be a python dictionary? can it just be a string?I tried using this script:
#Implement authentication using Cache Handler
What happens is:
I get redirected to
http://127.0.0.1:8000/accounts/spotify/login/callback/?code=xxxxxwhen I try to make an API call then the app fails because I have already logged in the user. I was guessing I did not have to authorize the app all over again because I already have the access token??@Peter-Schorn commented on GitHub (Aug 4, 2021):
Please use proper indentation when posting code. Otherwise, it becomes very hard to read.
The token info must be a dictionary with the following keys:
access_tokenrefresh_tokenexpires_at(a unix timestamp that represents when the access token expires)scope(must match the instance attribute of the same name of the auth manager; or, it could beNoneif you don't request access to any scopes)The reason why you're getting redirected to the authorization URL is because you're missing that last two parameters, so the token info is considered invalid. How is spotipy supposed to know when to refresh the access token if it doesn't know when it expires?
Furthermore, there are major issues with your cache handler. First of all, the first line of
get_cached_tokendoesn't make sense:token_infois not defined in this scope.Furthermore, in
SparisonCacheHandler.__init__,self.token_infois not defined iftoken_infoisNone. Instead the initializer should look like this:You also need to read the docstring for
CacheHandler.get_cached_tokenandCacheHandler.save_token_to_cache. The latter should returnNone; Your implementation returns the token info.Furthermore, you need to come up with one centralized place where the token info will be stored. It doesn't make sense to store it in both the cache handler and in the
SocialTokenobject, whatever that is. Pick one or the other. For example if you want to store the token info in theSocialTokenobject, then you could do something like this:@kwakubiney commented on GitHub (Aug 4, 2021):
@Peter-Schorn Thank you for your correction. I ended up implementing like this.
I do still get the same error from the beginning:
http status: 403, code:-1 - https://api.spotify.com/v1/me/tracks?limit=40&offset=0: Insufficient client scope, reason: NoneOther calls like
Spotify.me()works though which then means the scope doesn't get passed to theSpotifyAuthobject for some reason.@Peter-Schorn commented on GitHub (Aug 5, 2021):
This means that you didn't request the correct scope(s) when going through the authorization process. Show me the code where you implement the authorization process. It's impossible for me to help you if you don't do this.
Another way you can determine if the correct scopes are being requested is to look at the URL of the page that asks you to sign in with your Spotify account. You should see "user-library-read" in the query string of the URL.
These endpoints work because they don't require any scopes.
No, it doesn't mean that because you're not using
SpotifyOAuthto implement the authorization process (from what I understand). It means the scope is not getting passed to whatever object indjango-allauththat you are using to implement the authorization process.Also, there are still issues with your cache handler:
Why are you ignoring the
expires_atandscopeparameters? This won't work. You also should not be hardcoding the scope inSparisonCacheHandler.get_cached_token. This should be stored in yourspotify_object.@sapristi commented on GitHub (Nov 4, 2023):
I have set up a spotipy client using allauth token, see here https://github.com/sapristi/lalipo/blob/master/lalipo/spotipy_cache.py for how to implement the cache and override the client, and https://github.com/sapristi/lalipo/blob/master/lalipo/spotify_helpers.py#L9 for how to use it.
Note that allauth should be configured to store the token in db.