[ARCHIVED] Suddenly unable to get OAuth 2.0 access token in Canvas LMS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have an LTI plug-in which has been using OAuth 2.0 to authenticate/authorize. The tool has been working fine for 1 year but yesterday I faced a problem.
I am unable to get access token and hence the tool stopped working.
In summary, I use a GET request to get access code as below:
http://192.168.1.120/login/oauth2/auth?client_id=10000000000011&response_type=code&state=1234&redirect_uri=https://localhost:44399/Home/oauth2Response
Once I received code in the redirect Uri, I make a post request to get an access token. But I am not receiving access token now and my C# code is posted with the question. Could you please help me?
string json = string.Empty;
string urlCommand = "/login/oauth2/token";
try
{
using (HttpClient client = new HttpClient())
{
client.Timeout = TimeSpan.FromHours(1);
string url = "http://192.168.1.120/";
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
Dictionary<string, string> vars = new Dictionary<string, string>();
vars.Add("grant_type", "authorization_code"); //value will be either 'authorization_code' or 'refresh_token'
vars.Add("client_id", "10000000000011"); //this is the developer key "ID" value
vars.Add("client_secret", "nT2Icq7eSEQAFj6wqYE31F5iNgpi1gK2J7WC5Y6BN8c06pvroYRm2rwDkk2XuhM9"); //this is the developer key "Key" value
vars.Add("redirect_uri", "https://localhost:44399/Home/oauth2Response?code=" + code + "&state=1234"); //this is the developer key "URI" value
if (!string.IsNullOrEmpty(code)) vars.Add("code", code); //if canvas gave us a code, send it
string varString = JsonConvert.SerializeObject(vars);
HttpContent httpContent = new FormUrlEncodedContent(vars.ToArray());
using (HttpResponseMessage response = await client.PostAsync(urlCommand, httpContent))
{
if (response.IsSuccessStatusCode)
{
// excellent, canvas returned a success code, we should have also received token data, let's store the token data
json = await response.Content.ReadAsStringAsync();
ViewBag.accessToken = json;
}
else
{
}
}
}
}
catch (Exception ex)
{
}
This discussion post is outdated and has been archived. Please use the Community question forums and official documentation for the most current and accurate information.