I have an Azure app that returns a bearer access token. I use the Windows Account Manager (WAM) so there’s a special redirect URI. I’m using the following code to refresh the token but I’m getting a 400 response. I am sending the access code before it expires and I get the redirect URI right off Azure. I’m not certain though on the url of the PostAsync:
public async Task<string> RefreshTokenAsync(string refreshToken, string Tenant, string Instance, string ClientId)
{
using (var client = new HttpClient())
{
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "refresh_token"),
new KeyValuePair<string, string>("client_id", ClientId),
new KeyValuePair<string, string>("refresh_token", refreshToken),
new KeyValuePair<string, string>("redirect_uri", "ms-appx-web://microsoft.aad.brokerplugin/" + ClientId)
});
var response = await client.PostAsync($"{Instance}{Tenant}/oauth2/v2.0/token", content);
if (response.IsSuccessStatusCode)
{
var responseJson = await response.Content.ReadAsStringAsync();
var responseObject = JsonConvert.DeserializeObject<TokenResponse>(responseJson);
return responseObject.AccessToken;
}
else
{
string a = await response.Content.ReadAsStringAsync();
MessageBox.Show(a , "Error", MessageBoxButton.OK);
return string.Empty;
}
}
}
public class TokenResponse
{
[JsonProperty("access_token")]
public string AccessToken { get; set; }
}
Is there a simpler way to refresh a token?