Search code examples
c#desire2learnvalence

Valence D2l: Course Offrings from url using org unit id


I'm currently working on getting course offerings from org unit id using c#.

I'm brand-new to D2L valence. I have app ID/key pair and user ID/Key pair.

I am going to enter org unit id, get json response, parse the json response in c#, and output the associated course code and name.

string GET(string url)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        try
        {
            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                return reader.ReadToEnd();
            }
        }
        catch (WebException ex)
        {
            WebResponse errorResponse = ex.Response;
            using (Stream responseStream = errorResponse.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                String errorText = reader.ReadToEnd();
                // log errorText
            }
            throw;
        }
    }

this is my GET code. And, I'm trying to call it. The url and the main code are the following:

string url = "http://test.ca/d2l/api/lp/1.0/courses/644849";

GET(url);

The problem is I'm getting error says: The remote server returned an error: (403)Forbidden.

Also, I've tried this url:

string url = "http://lms.valence.desire2learn.com/d2l/api/lp/1.0/courses/644849";

This time, I got this error (Object reference not set to an instance of an object.)

I have app id/key pair and user id/key pair.

What should I do to solve this problem and end up with getting course offerings.

Thanks in advance, Phillip


Solution

  • The reason you're getting a 403 forbidden is because you are not sending the appropriate identifiers and signatures on your query string to allow your request to be authenticated (see http://docs.valence.desire2learn.com/basic/auth.html#id4).

    If you are using C#, I'd recommend using the Valence SDK located on Nuget to generate appropriate URLs. Take a look at https://github.com/Brightspace/valence-sdk-dotnet/tree/master/samples/Basic for a sample project using the SDK, specifically, https://github.com/Brightspace/valence-sdk-dotnet/blob/master/samples/Basic/Basic/Controllers/HomeController.cs, shows how you can use the SDK methods to make a whoami request.