SpotifyAPI-NET icon indicating copy to clipboard operation
SpotifyAPI-NET copied to clipboard

Exchange Token not working?

Open WoweePaw opened this issue 8 years ago • 9 comments

If I try to refresh the token to work for another +1h it only returns an empty string.

To refresh I'm saving the "payload.code" on AuthorizationCodeAuth to a string variable called "RefreshToken" which I'm using in my regentoken method.

this is the code I used there:

            if (token.IsExpired())
            {
                // Regen Token
                token = await auth.ExchangeCode(RefreshToken);
                _spotifyweb = new SpotifyWebAPI()
                {
                    TokenType = token.TokenType,
                    AccessToken = token.AccessToken
                };
            }

Code where I'm getting the RefreshToken from (based on example app)

private async void AuthOnAuthReceived(object sender, AuthorizationCode payload)
 {
            auth = (AuthorizationCodeAuth)sender;
            auth.Stop();

            token = await auth.ExchangeCode(payload.Code);
            RefreshToken = payload.Code;
            _spotifyweb = new SpotifyWebAPI()
            {
                TokenType = token.TokenType,
                AccessToken = token.AccessToken
            };
            GetProfile(_spotifyweb);
}

I guess I'm missing something since the latest (3.0.2) NuGet Update and I can't see whats missing so I would appreciate some hints ^^'

WoweePaw avatar Sep 10 '18 15:09 WoweePaw

RefreshToken = payload.Code; => RefreshToken = payload.RefreshToken;

JohnnyCrazy avatar Sep 10 '18 16:09 JohnnyCrazy

I think the problem is RefreshToken = payload.Code; because the refresh token should be stored in the Token object you get from RefreshToken = payload.Code;. Also I think token = await auth.ExchangeCode(RefreshToken); shouldn't work but I wasn't able to find the right method yet.

h-philip avatar Sep 10 '18 16:09 h-philip

Gnarf ... tripple facepalm. ^^' ... Thanks for the fast answers you two ❤️

/edit payload does not have RefreshToken :3 image

WoweePaw avatar Sep 10 '18 20:09 WoweePaw

Oh sorry, my bad, it's token.RefreshToken

JohnnyCrazy avatar Sep 10 '18 20:09 JohnnyCrazy

Ah ok ... I'll try that and respond in an hour if it worked or not ^^'

WoweePaw avatar Sep 10 '18 20:09 WoweePaw

Token is still empty after refresh. :(

Ausnahme ausgelöst: "System.NullReferenceException"

After 1 hour it stops working. :/ .NET Framework 4.6.2

WoweePaw avatar Sep 10 '18 21:09 WoweePaw

Oh I think @h-philip made a valid point, exchangecode is for the initial token, seems like I forgot to Port refreshToken 😐

Will update tomorrow

JohnnyCrazy avatar Sep 10 '18 21:09 JohnnyCrazy

Oh well then it makes sense ^^' thought it's the new way so I changed it on code :3 ... thanks for fixing in advance 👍 :)

WoweePaw avatar Sep 10 '18 22:09 WoweePaw

Fixed in 70a57579cf19d573fb7fbf041b99e20ee52c33f1

Simply use Token refreshedToken = auth.RefreshToken(token.RefreshToken)

JohnnyCrazy avatar Sep 21 '18 12:09 JohnnyCrazy