Forum Xamarin.Forms


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code or u

soufianemarlysoufianemarly Member ✭✭✭

Hello I'm using xamarin forms with office authentification
the authentification works fine but lately i receive this error message :
AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token.

with some research i find that i need to refresh the token. -->
the question is how to refresh the token can anyone guide me.

        public async Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri)
            Console.WriteLine("In Authenticate");
            AuthenticationResult authResult = null;

                var authContext = new AuthenticationContext(authority);
                if (authContext.TokenCache.ReadItems().Any())
                authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
                var uri = new Uri(returnUri);

                PlatformParameters platformParams = null;
#if __ANDROID__

                platformParams = new PlatformParameters((Android.App.Activity)Forms.Context);
#if __IOS__
                Device.BeginInvokeOnMainThread(() =>
                    UIViewController controller = new UIViewController();
                    controller = UIApplication.SharedApplication.KeyWindow.RootViewController;
                    platformParams = new PlatformParameters(controller);


                authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
                Console.WriteLine("Need to be filed : " + authResult);


            catch (Exception e)
                Console.WriteLine("Execption : " + e.Message);

            return authResult;

Thank you.

Best Answer


Sign In or Register to comment.