cancel
Showing results for 
Search instead for 
Did you mean: 
bertscjn1
Community Participant

Cross Post! Vanilla JavaScript API call doesn't work from webapp (401 error), but full URL works.

Jump to solution

Hello community! 

I am making an API call to Pages and pulling the default 10 pages no problem. However, when I try to add the "per_page=" parameter I get a 401 error in the console! What's strange is that if I copy and paste the entire API URL into the omnibar, the call is made without issue and I can see all the pages - published and unpublished. What am I doing wrong? Here is my code for reference:

var modules = {
"url": "[my institution instance]/api/v1/courses/sis_course_id:" + CourseID +"/pages?per_page=30?access_token=" + API_Key,
}

$.ajax(modules).done(function (response) {
console.log(response);
$("#Modulepages").append("<h2>Modules </h2><br>")
for (i=0; i<50 i++){
var content = response[i].title;
$("#Modulepages").append(content + "<br>");
}

})

Labels (6)
0 Kudos
1 Solution

Accepted Solutions
matthew_buckett
Community Participant

When building URL a '?' is used to separate the path (/api/v1/courses/sis_course_id:" + CourseID +"/pages) from the parameters, and the parameters should be separated by '&', in your example you use '?' to separate parameters so instead of:

"[my institution instance]/api/v1/courses/sis_course_id:" + CourseID +"/pages?per_page=30?access_token=" + API_Key,

it should be:

"[my institution instance]/api/v1/courses/sis_course_id:" + CourseID +"/pages?per_page=30&access_token=" + API_Key,

The reason it probably works in your browser is that in that situation your browser is also sending a cookie along with the request and that is performing the authentication rather than the API key.

View solution in original post

0 Kudos
2 Replies
matthew_buckett
Community Participant

When building URL a '?' is used to separate the path (/api/v1/courses/sis_course_id:" + CourseID +"/pages) from the parameters, and the parameters should be separated by '&', in your example you use '?' to separate parameters so instead of:

"[my institution instance]/api/v1/courses/sis_course_id:" + CourseID +"/pages?per_page=30?access_token=" + API_Key,

it should be:

"[my institution instance]/api/v1/courses/sis_course_id:" + CourseID +"/pages?per_page=30&access_token=" + API_Key,

The reason it probably works in your browser is that in that situation your browser is also sending a cookie along with the request and that is performing the authentication rather than the API key.

View solution in original post

0 Kudos

That worked! Thanks soooo much!!!