AnsweredAssumed Answered

Unable to download attachement of a course in c#

Question asked by dpbhatt on Mar 8, 2019
Latest reply on Mar 31, 2019 by dpbhatt

I had subscribed and installed "Blackboard Learn for REST and LTI Developers" on the AWS. I have to write code in c# to download files of a course.

 

The url to download an attachment is https://<Public DNS>/learn/api/public/v1/courses/uuid:d23e128e62f8483699c26836e06cab32/contents/_27_1/attachments/_13_1/download

 

When I directly paste the URL on the browser and hit enter, it displays the content of the file. Similarly, when I send a GET request with access_token on the Postman, I receive result without any problem. I observed the Request/Response in the browser and came to know that there are 2 subsequent requests before getting the result on the file.

 

However, I have not been receiving a result in my C# code. I am surprised to see StatusCode 500 i.e. "InternalServerError".

How to get the file contents in my case? Please help. Below is my code in C#.

 

using System.Net.Http;

namespace BlackboardLearn

{

    class Program

    {

        static void Main(string[] args)

        {

            // GET /learn/api/public/v1/courses/{courseId}/contents/{contentId}/attachments/{attachmentId}/download

 

            string fileUrl = "https://<domain name>/learn/api/public/v1/courses/uuid:d23e128e62f8483699c26836e06cab32/contents/_27_1/attachments/_13_1/download";

 

            using (HttpClient client = new HttpClient())

            {

                client.DefaultRequestHeaders.Authorization =

                    new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "access_token goes here");

                byte[] result = null;

                HttpResponseMessage response = client.GetAsync(fileUrl).Result;

                if (response.IsSuccessStatusCode) // Returns false as response is InternalServerError

                {

                    result = response.Content.ReadAsByteArrayAsync().Result;

                }

            }

        }

    }

}

Outcomes