With some help from various wonderful people on the Canvas Developers forum, the code below will query the Canvas API as the logged in user and log to the console(!).
Some things to note:
- I’m using the fetch() method as I like working with promises – fingers crossed that it’ll work in the app!
- using James Jones csrf extracting function to populate the X-CSRF-Token header;
- using “Accept”: “application/json” to tell Canvas that we want json and it then doesn’t include the while(1) stuff (try it without to see what I mean)
- using credentials: ‘include’ to tell fetch() to include the Cookie in the request as this is what Canvas seems to expect
The next step is to do something useful with the api – the first goal is to add next and previous buttons to pages linked to in the mobile app (currently you will only see these if you launch into a page through the app-specific Modules page).