ImplicitGrantAuth loses authentication after a while
I wrote a programm which uses both, web and local api. I just need the webapi to get fulltrack information and got the authentication process to work with ImplicitGrantAuth. After a while though, it won't work anymore. It seems like the auth expires.
Is there a way to extend the auth? I read on the Spotify API-Doc that there is something called "refresh_token" but I'm not sure if its documented in your library or if it's even possible to refresh the authentication.
This is the code which connects when a button is clicked.
private async void SpotifyWebConnect()
{
WriteConsole("Trying to connect so Spotify", null);
WebAPIFactory webApiFactory = new WebAPIFactory(
"http://localhost",
8000,
"XXXXXXXXXXXXXXXXXXXXX",
Scope.None,
TimeSpan.FromSeconds(20)
);
try
{
_spotifyWeb = await webApiFactory.GetWebApi();
}
catch (Exception ex)
{
WriteConsole(ex.Message, ex);
}
if (_spotify == null)
return;
}
and after a while
FullTrack fulltrack = null;
fulltrack = _spotifyWeb.GetTrack(SPOTIFY_URI);
always returns null.
Hi, the token does indeed expire after some time. As far as I know you can't refresh the token with your auth method("ImplicitGrant").
You can either:
- do the authentication again
- use "AutorizationCodeAuth" (Doc)
I see. I'm kinda lost on the key-exchange that way. I'm fine with havin to authorize every time I start the app but I don't wanna do it every x minutes/hours.
If someone is willing to help I'd be down for that.
Yes, ImplicitGrant only works for about 30 minutes.
You will either need to use the kinda hacky and super insecure internal HTTP Server together with AutorizationCodeAuth, or use an actual server backend. All Spotify API Authentication & Authorization flows are not designed for long running desktop clients, which makes it hard to write stuff without a server.
For example, if you decide to use the internal HTTP Server + AutorizationCodeAuth, you will need to give clients your client_id AND client_secret and furthermore add a localhost domain to your redirect_urls in your spotify dasboard --> Anyone can use those keys and either access user-data or spam via your API keys. Not good stuff...
So if I understand it correctly I need an external HTTP Server which handles the key exchange? If so are there some examples that show how to accomplish this? Getting the HTTP Server is no problem for me but I have to communicate between the app and the server then.
So with the local api being dead now. Is there some more documentation on the AutorizationCodeAuth more eplicit on the PHP key-exchange. Some more in depth examples or tutorials would be extremly helpfull here.
@Inzaniity Hello. Have you solved your problem? I'm in the same situation and looking for some help. Or anyone can show a example of how use AutorizationCodeAuth?
All of the information you need is in the Spotify Authorization Guide. Additionally, Spotify use OAuth, which is widely documented across the Internet. You might be creating an application only to be used by yourself, but getting user auth correct is important because you're handling tokens which can be used to access someone's account if you leak them or leave them lying around.
I'm fine with havin to authorize every time I start the app but I don't wanna do it every x minutes/hours.
This isn't how OAuth and refresh tokens work. You shouldn't need to authorize every time you start the app, nor every X minutes/hours. Once the user authorizes once inside your app, you receive a refresh token which is valid forever (or until a user de-authorizes your app in their Spotify account). Exchanging the refresh token for an access token once the one you're using has expired is simply a single API call and involves no interaction from the user.
Once i get the token with
`Token token = auth.ExchangeAuthCode(response.Code, "XXXXXXXXXXX");
var spotify = new SpotifyWebApiClass()
{
TokenType = token.TokenType,
AccessToken = token.AccessToken
};`
How to refresh the token? And how to know when it must be refreshed? Thanks in advance.
Please have a look at the methods provided by those objects before asking, VS should help you out in a lot of times:
https://github.com/JohnnyCrazy/SpotifyAPI-NET/blob/df53f75368717e9b071fe9f97ad8becfcc73a5ee/SpotifyAPI/Web/Auth/AutorizationCodeAuth.cs#L45
https://github.com/JohnnyCrazy/SpotifyAPI-NET/blob/df53f75368717e9b071fe9f97ad8becfcc73a5ee/SpotifyAPI/Web/Models/Token.cs#L20
I'm completly stucked with this. Need some help, please
From this code in the auth_OnResponseReceivedEvent event:
token = auth.ExchangeAuthCode(response.Code, clientSecret)
_spotifyWeb = New SpotifyWebAPI() With {
.TokenType = token.TokenType,
.AccessToken = token.AccessToken
}
I take token.RefreshToken and i save it in a variable named refreshToken for use later with token.RefreshToken.
When i do token = auth.RefreshToken(refreshToken, clientSecret) i get a new token in token.AccessToken, different for the first i got in auth_OnResponseReceivedEvent event.
I do this every 20 minutes but after 3600 secs when token expires, API doesn't respond anymore. What i need to do in order to work for more than 3600 secs with token.RefreshToken?
Another question is: can i change token.ExpiresIn to a lower value for make the tests?
If i change with token.ExpiresIn = 10, with token.ExpiresIn i got 10, but seems do not use this value.
I will appreciate some help for this. Thanks in advance.
I do this every 20 minutes but after 3600 secs when token expires, API doesn't respond anymore.
There's no need to refresh your token every 20 minutes when they last for 60. Either refresh it once the API tells you the token has expired, or set up some sort of timer to refresh the token just before that (say at 55-59 minutes). Then ensure that you're updating the access token which is actually being sent to the API. It's not enough to simply refresh the token, you need to actually make sure your API requests are being sent by updating the access token which is being used by your SpotifyWebAPI instance.
Then ensure that you're updating the access token which is actually being sent to the API. It's not enough to simply refresh the token, you need to actually make sure your API requests are being sent by updating the access token which is being used by your SpotifyWebAPI instance.
I understand this. And after call refreshToken, i see that mySpotifyWebAPI.AccessToken is the new token i have obtained. And i see that is not the same i got when i create the mySpotifyWebAPI.AccessToken instance.
That's what i don't understand. If mySpotifyWebAPI.AccessToken is the new token offered by refreshToken, mySpotifyWebAPI.AccessToken should use the new token... not?
(Thanks for your comment)
Solved. Although mySpotifyWebAPI.AccessToken has the new token after call refreshToken, i need to set mySpotifyWebAPI.AccessToken = token.accesstoken after call refreshToken. And then, all is working. Curious...