@a_mottura , @adamwarecx
I spent a few minutes (15 or so) playing around with this tonight and might have figured something out. As I started to write it out, I figured out it was only successful if I loaded it within a browser, so you might need to use a headless browser to make the calls.
- Get the submission information through the Submissions API and look at the Preview URL.
- Call the Preview URL. You will get an HTML response, not JSON, that says "You are being redirected." but there is a link in there. There is also a Location header that you can use from the redirect.
- Load the redirected page. This is NOT an API call, do not include the authorization header. This returns all the sessionData information, the name, and all the other stuff that is needed.
- Download the annotated PDF. Step 3 contains an annotated_pdf_download relative path or you can take the current URL and change the last part to /view/annotated.pdf or maybe /view/annotated.pdf?dl=1 or possibly just /annotated.pdf. Note that this is not an API call.
Here are some screen shots to help with the process (and to confirm that I have it right).
Get the submission information.
GET <instance>/api/v1/courses/896851/assignments/4994189/submissions/2175488
If there is an attachment, then the attachments property is an array. It should have a preview_url that looks something like this (URI decoded and line breaks added)
/api/v1/canvadoc_session?
blob={
"moderated_grading_whitelist":null,
"enable_annotations":true,
"enrollment_type":"admin",
"anonymous_instructor_annotations":false,
"submission_id":35142732,
"user_id":10000008296700,
"attachment_id":112491023,
"type":"canvadoc"
}
&hmac=<censored>
Call the preview URL.
Note that this is an API call so you'll need to include the token. I did not try playing around with the parameters in the blob, but there's an hmac code at the end to sign it as authentic, so you may not be able to. Luckily for you, the enable_annotations is true.
This comes back with a 302 FOUND error and a redirect. The location header looks like this:
https://canvadocs.instructure.com/1/sessions/
<really long code, perhaps a JWT access token>
/view?theme=dark
You can also extract it from the href in the body.
Load the redirected page
This is not an API call, don't use the authorization header that you normally use, but be sure to honor its request to set-cookie. This is HTML with an embedded script that sets window.DocViewer. Part of that is sessionData. Inside sessionData is an annotated_pdf_download and annotatedPdfUrl (for the pdfjs viewer) properties.
Note window.DocViewer is not JSON, it's JavaScript, so you might need to access window.DocViewer.sessionData.urls.annotated_pdf_download to get it. Alternatively, just take the current location and change the end to /view/annotated.pdf
Unfortunately, you cannot load either annotated_pdf_download or annotatedPdfUrl without letting the JavaScript on the page run (maybe it's establishing the session?) Anyway, without this step -- which I wasn't able to track down using Advanced REST Client (some use Postman) -- it wouldn't work. But if I load the page in the browser, even one I'm not logged into Canvas in, then it works.
It also has a URL for pdf_download (without the annotations).
Download the PDF
Once you have the correct address and things have been initialized, you can GET the path (it's a relative path without the hostname, so you may need to add the canvadocs.instructure.com to it).
Notes
Like I said, step 3 is the stumbling block. If I paste that URL into a different browser and then try the annotated_pdf_download link, I can get it. That's why I think a headless browser might work. I've also used Node with jsdom in some other cases (getting RollCall data out) and it may work here. Other people use selenium, but I don't know enough about it to recommend it.
It's now well over an hour after I started this and bed is calling (about 4 hours ago). I have other things that need done, so I won't be able to work on this right now, but hopefully this will get you closer to the goal.
You'll need to repeat this process for every submission, there's not a "download all submissions with annotations." There is a feature request for it: https://community.canvaslms.com/ideas/6232-bulk-download-of-annotated-crocodoc-documents-by-assignme...
This discussion post is outdated and has been archived. Please use the Community question forums and official documentation for the most current and accurate information.