null access_token

Jump to solution
donsrle
Community Novice

I am trying to create 3rd party app following LTI standard. I am hosing canvas locally on VM. I am using self-signed certificate and set https requests to external tool.

I need token in order to make API calls to Canvas.

I am stuck at the step 3 of getting access_token. Even though I am able to complete step 1 (passing 'scope=/auth/userinfo') and get the code when I try to POST that code in step 3 to /login/oauth2/token along with additional parameters from spec OAuth2 - Canvas LMS REST API Documentation ,  I am getting JSON response in format:

{

     "access_token": null,

     "user: { "id" : 1,

                   "name": "mail@address.com"

     }

}

This is my code (c#):

public static string GetAccessToken(string code)

        {

            string retVal = String.Empty;

            string returnUrl = "https:localhost:63333/httphandlers/OAuthComplete.ashx";

            string fullUrl = "http:localhost:3000/login/oauth2/token";

            var request = (HttpWebRequest)WebRequest.Create(fullUrl);

          

            request.Method = "POST";

            var postData = string.Empty;

             // adding post data

            postData = UIHelper.AppendRequestParameter(postData, "client_id", ConfigurationManager.AppSettings[Constants.AppSettingsKeys.DeveloperClientId]);

            postData = UIHelper.AppendRequestParameter(postData, "client_secret", ConfigurationManager.AppSettings[Constants.AppSettingsKeys.DeveloperSecretKey]);

            postData = UIHelper.AppendRequestParameter(postData, "grant_type", "authorization_code");

            postData = UIHelper.AppendRequestParameter(postData, "redirect_uri", HttpUtility.UrlEncode(returnUrl));

            postData = UIHelper.AppendRequestParameter(postData, "code", code);

            var data = Encoding.ASCII.GetBytes(postData.TrimStart('?'));

            request.ContentType = "application/x-www-form-urlencoded";

            request.ContentLength = data.Length;

            using (var stream = request.GetRequestStream())

            {

                stream.Write(data, 0, data.Length);

            }

            var response = (HttpWebResponse)request.GetResponse();

            retVal = new StreamReader(response.GetResponseStream()).ReadToEnd();

            return retVal;

        }

It could be something trivial but I am not able to figure it out why I am not able to get access_token.

Thanks in advance,

Srdjan

1 Solution

After lot of testing, I finally found the solution and could get the access_token. I stopped passing the string 'auth/userinfo' as the scope parameter in the GET login/oauth2/auth request and the POST request returned the expected access token value.

I hope it can help you, because I think that it is not clearly described in the documentation.

View solution in original post