Auth0 Home Blog Docs

Oauth/token unauthorized issue

I have set up my function when it is hit, to check if the current access token has expired (from cache). If the access token has expired, I use the refresh token to request a new access token. I am however running into an issue here when I try to hit the “oauth/token” url, getting an unauthorized error: “The remote server returned an error: (401) Unauthorized.”. The request body, method and content type are set up correctly following the https://auth0.com/docs/tokens/refresh-token/current#use-a-refresh-token guidelines

code:
image

Since the content type is x-www-form-urlencoded, there shouldn’t be any JSON format involved. You’re creating a JSON string which you then serialize. That doesn’t look right, and seems to be the problem.

Not too familiar with C# or the HttpWebRequest class in particular, but check the C# code example on https://auth0.com/docs/tokens/refresh-token/current#use-a-refresh-token, and how the parameters are set there:

request.AddParameter("application/x-www-form-urlencoded", "grant_type=refresh_token&client_id=%24%7Baccount.clientId%7D&client_secret=YOUR_CLIENT_SECRET&refresh_token=YOUR_REFRESH_TOKEN", ParameterType.RequestBody);

@mathiasconradt Ow, my bad! Must have tunnel visioned when I was going through it, I changed the format as suggested and everything works as expected now, thank you! :slight_smile:

image

1 Like

Glad that it solved it :+1: