cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
daniel_olivas
Community Participant

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,

16 Replies
James
Community Champion

I used ARC (advanced rest client) to send this request to

POST /api/v1/accounts/self/courses

{
  "course": {
    "name": "Test course for Daniel"
  }
}

It worked. Then I modified the "self", which is the root account, to be one of our actual subaccounts and it worked there, too. Since the format is good, it may be the Google App portion that is causing the problem. When I wrote it, I wasn't doing much in the way of POST, so it wasn't as fully tested as the GET or PUT options.

I have to go pick up the kids, so it will be a while before I can test it in Google Apps.

James
Community Champion

 @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

daniel_olivas
Community Participant

James,

Thank you for the detailed response.  I will try this out today.  

I just wanted to confirm that I got the second part of your modification correct by adding the else if here:

298503_pastedImage_1.png

Thank you,

daniel_olivas
Community Participant

I still can't figure this one out.  I've tried your suggestions but I keep getting the same result.

I had to change:

parms.payload = JSON.stringify(payload);

To 

parms.payload = JSON.stringify(parms.payload);

 @James ‌

James, do you mind showing me what your results variable response is? 

James
Community Champion

It looks like you're running an old version of the script that I wrote ... or that someone else has modified it. I don't have a parms.payload = null in there, but yes, you put it in the right spot. That said, you may be missing some other stuff and I'm not sure what you have for the initial if that you have the else to.. You're showing the code at line 148 and it's around line 300 in mine.

James
Community Champion

This strongly suggests that you don't have the latest version of the script I am using. The latest version of my script, without the patches shown here, can be found in the script linked from here: Adjust All Assignment Dates on One Page‌. There's been some change since it originally came out, including advancements to the way that putting in your access tokens is done. That may make it unusable for you, but you can see how things are working and what you're missing. 

daniel_olivas
Community Participant

Thanks James, 

Using the updated version of your script, along with the above recommended changes worked for me.  I appreciate it.