Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request new access token and resubmit the api request

I am using the Refit library with my Xamarin forms project to send API requests. It works great, but have an issue when the access token expires.

When the access token expires, I get an 401 error from the server, as expected. I then make a call to the Identity Server to issue a new access token, but I am having difficulty in resubmitting the API request. I still get unauthorised error. Appreciate some help.

I have created an AuthenticatedHttpClientHandler class to handle the token.

public class AuthenticatedHttpClientHandler : HttpClientHandler
{
    private readonly string _token;

    public AuthenticatedHttpClientHandler(string token ) 
    {
        _token = token;       
    }

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var auth = request.Headers.Authorization;
        if (auth != null && !string.IsNullOrWhiteSpace(_token))
        {
            request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, _token);
        }
        else
        {
            request.Headers.Remove("Authorization");
        }
        var result = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
        if (result.StatusCode == System.Net.HttpStatusCode.Unauthorized )
        {
            IdSrvApiService idsrvApiService = new IdSrvApiService();
            RefreshTokenService refreshTokneService = new RefreshTokenService(idsrvApiService);

            if( Settings.RefreshToken != ""){
                var newToken = await refreshTokneService.RefreshAccessToken(Priority.Background).ConfigureAwait(false);
                TokenHelper.CacheToken(newToken);
                request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, Settings.AccessToken);
                return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
            }
            else
            {
                return result;
            }
        }
        else
        {
            return result;
        }
    }
}
like image 925
Libin Joseph Avatar asked Jan 08 '18 11:01

Libin Joseph


People also ask

How do I get a new API token?

Generating an API tokenClick the Settings tab, and make sure Token Access is enabled. Click the Add API token button to the right of Active API Tokens. The token is generated and displayed. Enter an API token description.

How do I refresh OAuth access token?

Step 1 − First, the client authenticates with the authorization server by giving the authorization grant. Step 2 − Next, the authorization server authenticates the client, validates the authorization grant and issues the access token and refresh token to the client, if valid.


1 Answers

I would suggest inspecting the raw request sent to see if the one sent after refresh token has the proper headers sent.

There is also the chance that you are sending the wrong token on the second try. Confirm that the value in newToken is what is being used via Settings.AccessToken

like image 103
Nkosi Avatar answered Oct 13 '22 22:10

Nkosi