Canvas API - Attempting to get Analytics for Assignments as Student

Jump to solution
AtticusMcNulty
Community Member

Hi! I am a student messing around with the canvas API using React and seeing all the different things I can access from my classes. I wanted to be able to get the grades and statistics for each of the assignments in my courses, but have been running into "user not authorized to perform that action" errors. I was wondering if there is a workaround to this, or if I am doing something wrong in my code.

Note: I have been using my personal API key to access this information, but have replaced in the code below for privacy. One image is of my server.js file, another is of my main React component. Thank you in advance for any help!

0 Likes
2 Solutions
sendres
Community Participant

Hi @AtticusMcNulty,

First of all, kudos to you for your experimentation! It's great to see students hacking in the benevolent sense of the term, meaning to learn the capabilities of a system and leverage that knowledge for innovative, unexpected, and beneficial purposes.

The Canvas API will allow the same actions that are available to you through the web interface, and likewise block actions that are not allowed to you through the web interface. The fact that you are getting the "user not authorized" message is evidence of this, and actually means that your code is working.

Out of the box, Canvas has default permissions for each of its five pre-installed roles. These settings are managed by the Canvas administrator for your institution, who may have made some changes from these defaults. (But probably not by much, since the defaults are quite reasonable.)

You'll note that the ability of students to view analytics is turned off by default. This is likely so that, in a small class, you can't determine other students' grades. For instance, if you're in a class of only two students, it would be simple for you to use your analytics view to figure out how your classmate scored on their assignments.

Here are some fun things you can (probably, if you have permission) do with the API:

  • Pull down a roster of the people in your course. Canvas will only send you 10 enrollments by default, with a link in the response header to get the next page of users. Fetching a course roster is is a good way to understand how to handle pagination that happens in many Canvas API responses.
  • Use the user information to send a conversation message to a classmate. (It might be a good idea to get their permission first.)
  • Retrieve all the posts in a discussion and build your own web view of the thread.

Have fun and be sure to post back if you have more questions!

Steven Endres (he/him/his)
Director of Learning Technologies
Information Technology (Lewis 001A)
Dominican University | 7900 Division Street | River Forest, IL 60305
Mobile: (708) 466-9452 | sendres@dom.edu | Book a meeting with me
.................................
We are DominicanWe Go First.

 

 

View solution in original post

Hi @AtticusMcNulty,

Yes, this is something you can do. It's not exactly simple, but the good news is that you can work toward this goal in a step-by-step fashion. If you can get through this process, you will have learned a lot and have a nice project to show a professor or prospective employer demonstrating your knowledge of REST API.

Here's an outline of the steps you'd need to do.

  1. Become familiar with the Canvas API
  2. Create an access token so your program can talk to Canvas
  3. Test your token
  4. Get a list of your courses
  5. For each course, get a list of your assignments
  6. For each assignment, get a list of your submissions, which will include the grade you earned

Ready? Get some caffeine, because we'll need it!

Become Familiar with the Canvas API

Before diving into programming, I recommend becoming familiar with the Canvas API. Here's a simple way to get started.

  1. Launch Mozilla Firefox. (Other browsers might do this too, but I know Firefox makes these steps easy.)
  2. Log into Canvas and go to your Dashboard. Your address bar will probably say something like https://myschool.instructure.com. This is the base URL of your Canvas instance. Copy this address to your clipboard.
  3. Open a new tab. Paste in the base URL, then append /api/v1/users/self, making the address bar read something like https://myschool.instructure.com/api/v1/users/self. Hit Enter. You should see a JSON response with some basic information about your Canvas user account, formatted in pretty blue, pink, and green colors. If so, you've successfully made a call using your browser to the Canvas API. In this case, because you were logged into Canvas in one tab, your browser included this authorization when it made the API request in another tab, so Canvas was able to allow and respond to the request.

Now, we can start writing a program to do get the same information...

Create an Access Token

First, Canvas will need to see that your program is authorized to request information on your behalf. By far, the easiest way to do this is manually generate an access token.

  1. Log into Canvas, click Account, then click the Settings link on the pop-out panel.
  2. Under the Approved Integrations section, click the + New Access Token button.
  3. Enter a Purpose ("testing" is fine), enter an expiration date if desired, and click Generate Token.
  4. The system will show your new token. Record this value immediately, because once you leave this screen, you won't be able to retrieve the full token anymore, and you'd have to regenerate it to get a different value. This token is essentially equivalent to your Canvas username and password combined together in a single value, so safeguard it accordingly!! I recommend NOT hard-coding this information into your program Instead, learn about using environment variables with your language/platform of choice to store this information. These will let you save your top-secret credential in the user profile on your computer or perhaps in a separate file in the project folder on your hard drive. (This file is one you would NEVER share with anyone else, nor commit to a Git repository of your code, for example.)

Test Your Access Token

Second, you can then use your access token to connect to Canvas. Each programming language will have a way to make HTTP requests of different types, with GET, POST, PUT, UPDATE, and DELETE being the most common. You''ll use your language's request function to authenticate to Canvas with your access token and make a request. I recommend making a basic GET request to the same endpoint you used above, /api/v1/users/self, and printing to the screen the JSON data you get back. Once you're able to retrieve your user information, you can move on to something more advanced.

Get a List of Courses

First, from your Dashboard, go visit some of your courses, and note the URLs in your browser address bar. You'll see that each address ends in /courses/<some_number>. These numbers are course ID numbers and are unique to each course.

Now use the API endpoint /api/v1/users/self/courses to get a list of your courses. Try this first in the browser (adding it after your base URL, of course) to see what information you get back. Then modify your program to call this endpoint and produce a list of your courses. There are a few things you'll want to note here:

  1. Each course listed in the API response has an id parameter that corresponds to the course ID in the address bar when you browse to the course.
  2. By default, the API will only return 10 courses, even if you're enrolled in more. This is the result of pagination, a process whereby Canvas breaks its response into a number of pages. What do you do if you are enrolled in more courses and want a list of all of them? When you get a Canvas response, it will include a header to the next page of results (if there is another page). So the way to handle this is to retrieve the first page, see if it has a next header link and, if so, follow that link to the second page. You repeat this process until Canvas gives you a response that doesn't have a next page, at which point you know you've gotten all the results.
  3. The API will return past, present, and future courses in its response. Presumably, you want to get your grades for current courses and don't care (much) about reporting assignment grades for past courses. Or do you? Figuring out whether and how to filter your reporting to current courses based on the API data is left as an exercise to the reader.

Get a List of Assignments from Each Course

Once you have a list of your course IDs, you can use these to get a list of assignments in each course using this endpoint: /api/v1/users/self/courses/<course_id>/assignments. These responses will include the following valuable information:

  1. The id for each assignment.
  2. The grading_type, which is typically 'points' but can also be 'pass_fail', 'percent', 'letter_grade', or 'gpa_scale'.
  3. The points_possible maximum score for the assignment, if the grading_type is points.

Onward to the last step...

Get the Grade for Each Assignment Submission

Finally, you can get the grade for your submission to each assignment using api/v1/courses/<course_id>/assignments/<assignment_id>/submissions/self. These will include the actual grade for the assignment (if one has been assigned).

Conclusion

If you made it this far, you should be very proud of yourself. This is not a trivial project, especially when you're just starting out. Give these steps a try and feel free to ask more questions!

Steven Endres (he/him/his)
Director of Learning Technologies
Information Technology (Lewis 001A)
Dominican University | 7900 Division Street | River Forest, IL 60305
Mobile: (708) 466-9452 | sendres@dom.edu | Book a meeting with me
.................................
We are DominicanWe Go First.

View solution in original post

0 Likes