Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unexpected stale token response #281

Open
wkeuning opened this issue Feb 26, 2020 · 1 comment
Open

unexpected stale token response #281

wkeuning opened this issue Feb 26, 2020 · 1 comment

Comments

@wkeuning
Copy link

wkeuning commented Feb 26, 2020

Hi,

When I make an API request with a token that is expired I receive the following error:

FitbitTokenException: In interceptor OAuth2AutoRefreshInterceptor inside method InterceptResponse we received an unexpected stale token response - during the retry for a call whose token we just refreshed 401

I traced this to the following method in OAuth2AutoRefreshInterceptor.cs:

public async Task<HttpResponseMessage> InterceptResponse(Task<HttpResponseMessage> response, CancellationToken cancellationToken, FitbitClient Client)
        {
            if (response.Result.StatusCode == System.Net.HttpStatusCode.Unauthorized)//Unauthorized, then there is a chance token is stale
            {
                var responseBody = await response.Result.Content.ReadAsStringAsync();

                if (IsTokenStale(responseBody))
                {
                    Debug.WriteLine("Stale token detected. Invoking registered tokenManager.RefreskToken to refresh it");
                    await Client.RefreshOAuth2TokenAsync();

                    //Only retry the first time.
                    if (!response.Result.RequestMessage.Headers.Contains(CUSTOM_HEADER))
                    {
                        var clonedRequest = await response.Result.RequestMessage.CloneAsync();
                        clonedRequest.Headers.Add(CUSTOM_HEADER, CUSTOM_HEADER);
                        return  await Client.HttpClient.SendAsync(clonedRequest, cancellationToken);
                    }
                    else if (response.Result.RequestMessage.Headers.Contains(CUSTOM_HEADER))
                    {
                        throw new FitbitTokenException(response.Result, message: $"In interceptor {nameof(OAuth2AutoRefreshInterceptor)} inside method {nameof(InterceptResponse)} we received an unexpected stale token response - during the retry for a call whose token we just refreshed {(int)response.Result.StatusCode}");   
                    }
                }                
            }

            //let the pipeline continue
            return null;
        }

When debugging I verified that Client.RefreshOAuth2TokenAsync(); does actually refresh the AccessToken.
However clonedRequest uses the old AccessToken in the Authorization header and not the newly refreshed AccessToken, therefore the request fails again and this triggers the 'unexpected stale token response'-FitbitTokenException as the request now contains the custom header.

Adding clonedRequest.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Client.AccessToken.Token);

here:

if (!response.Result.RequestMessage.Headers.Contains(CUSTOM_HEADER))
{

	var clonedRequest = await response.Result.RequestMessage.CloneAsync();
	clonedRequest.Headers.Add(CUSTOM_HEADER, CUSTOM_HEADER);
	clonedRequest.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Client.AccessToken.Token);
	return  await Client.HttpClient.SendAsync(clonedRequest, cancellationToken);
}

fixes this.

@dsurrao
Copy link

dsurrao commented Jan 20, 2023

I am getting the same error. Is there a fix planned for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants