cancel
Showing results for 
Search instead for 
Did you mean: 
daniel_olivas
Surveyor

Create a course using the API

Jump to solution

I'm borrowing code from  

I'm not sure how to pass the course parameters.  I've tried multiple ways.

apiCall = canvasAPI("POST /api/v1/accounts/1/courses?course\[name\]=The Best Course Ever");

and also like this:

function myFunction() {
   var j = {
         "name": "Another Test Course",
         "account_id": 201,
         "workflow_state": "unpublished"
   }

    var myJSON = JSON.stringify(j);
    var apiCall = canvasAPI("POST /api/v1/accounts/201/courses",myJSON);
}

When I run both of these, the response looks like all the courses i'm enrolled in, but the no course gets created.

Does anyone have sample code, using Google Script, that creates a new course?  Or any sample code that uses the API to create a new course?

Thanks,

1 Solution

Accepted Solutions
James
Navigator

 @daniel_olivas ,

I finally got around to testing this some. 

I added the content-type and accept headers to the Authorization header. Technically, the Accept header shouldn't be required, but it will keep Canvas from sending while(1); at the start of the output if it's there.

var parms = {
  'headers': {
    'Authorization': 'Bearer ' + userProperties.token,
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  }
};‍‍‍‍‍‍‍

I then modified the code for the POST/PUT portion from just payload to JSON.stringify(payload).

    } else if (parms.method == 'post' || parms.method == 'put') {
      parms.payload = JSON.stringify(payload);
    }‍‍‍

Then I called it successfully with this code

var obj = {
  "account": "self",
  "course": {
    "name": "Test course for Daniel"
  }
};
var results = canvasAPI('POST /api/v1/accounts/:account/courses', obj);

I have only tested it with creating a course and a wiki page, so I don't know what effects, if any, it will have on other things.

View solution in original post

16 Replies
chofer
Community Coach
Community Coach

Hello there  @daniel_olivas ...

I don't have an answer for you, but I wanted to let you know that I am going to share your question with the Canvas Developers‌ group here in the Canvas Community in hopes that your question will get some additional exposure.  If you are not yet following this group, please use the link that I have provided, and then click on the "Follow" button at the top right corner of the page.  Also, you'll find an "Actions" button near the top right corner of the page.  Click on that, and then click on "Join group". I hope this will be of help to you, Daniel.  Good luck!

James
Navigator

 @daniel_olivas ,

Your request is improperly formatted. The documentation to create a course is found in the Create a new course endpoint of the Courses API.

There, you'll see that all of the items begin with course. That means that you need an course object that contains all the information.

Instead of the j that you have, try this:

{
  "course": {
    "name": "Another Test Course"
  }
}

The course[account_id] isn't necessary since you specify the account_id in the path.

I've not seen workflow_state in a request, but there is an offer option (which doesn't appear to be under the course object)

Your first example is weird -- in that the payload to a POST is not normally specified in the query string. The problem there may be that you didn't URL encode it. But about 13:10 in the video, Jacob mentions that he started with my code and I'm pretty sure that I didn't write it handle a query string with a POST.

James,

Thanks for responding.  I tried your suggestion, but I'm getting the same result, no course created, and response that doesn't make sense.

Here's my code:

function myFunction() {
   var j = {
         "course": {
            "name": "Daniel Test Course"
         }
    }

   var myJSON = JSON.stringify(j);
   var apiCall = canvasAPI("POST /api/v1/accounts/201/courses",myJSON);
   Logger.log(apiCall);
}

The apiCall response is a JSON response with every course I'm enrolled in. 

I'm not an expert at canvasAPI, obviously, but other examples that I've seen using it just pass the array, not a JSON string of the array.  I would suggest trying to pass j instead of myJSON to the apiCall.

Hey Russell, I tried that already.  Same result. 

Thanks though,

daniel_olivas
Surveyor

Here's what the header contains in the POST:

{headers={Authorization=Bearer 'MyToken', Content-Type=application/json}, method=post, payload={course={name=Daniel Test Course}}}

MyToken = my actual token value.

I've also tried this, but I get this error: {"status":"unauthenticated","errors":[{"message":"user authorization required"}]}

function myFunction() {
  var data = {
       'course' : {
            'name' : 'Daniel Test Course'
      }
  };
var options = {
    'Authorization' : 'MyToken',
    'method' : 'post',
    'contentType': 'application/json',
    'payload' : JSON.stringify(data)
};

var r = UrlFetchApp.fetch('https://MyDomain/api/v1/accounts/201/courses', options);
}

No longer gettingthe authorization error once I formated the options like this:


var data = {
   'course' : {
         'name' : 'Daniel Test Course'
    }
};
var options = {
   'headers' : {
      'Authorization' : 'Bearer 8222~RgbECk3uU9Xv4834JFbnxKX38NnREDnAa4miiRDJUXgkJeEQdREShwYNldrrq83p',
      'contentType': 'application/json',
   },
   'method' : 'post',
   'payload' : JSON.stringify(data)
};

But the course still isn't being created:

This is the response I keep getting:

298101_pastedImage_3.png

Your auth code is showing in this post, if that's real then be sure to expire the existing one right away.

Some of my code automatically converts JSON by stringifying it, so you do pass the parameter. I would have to dig into it more, I'm between classes right now.

I deleted the auth code and created new one.  

Thanks for your help with this.