Community

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
ksz
New Member

uploading a file to Canvas through API

Jump to solution

I'm trying to perform a post to

POST /api/v1/users/:user_id/files
to upload a profile picture for each student.

I get an exception in the first step as outlined here:

Uploading Files - Canvas LMS REST API Documentation 

exception that I get is:

{"Http Exception occurred when calling Canvas. StatusCode Unauthorized, Url: users/11308/files, Json sent: {\"name\":\"80508_profile_picture.jpg\",\"size\":9361,\"content_type\":\"image/jpeg\"}"}

Method: POST, RequestUri: '<our server address>/api/v1/users/11308/files'

I can do other api calls like get a user, update a users etc. so I know it's not an authentication issue. Why is it giving me an unauthorized error? Currently we show profile pictures in Canvas by supplying a url to an external source. In order to get that to work our servers had to be white listed. Is it the same situation with uploading files? Is there something that needs to be enabled in order for this to work?

Thanks

Labels (1)
0 Kudos
1 Solution

Accepted Solutions
pklove
Community Champion

Are you doing this with an admin token?

"Note that typically users will only be able to upload files to their own files section".

So if doing this as an admin, add:

    ?as_user_id=user_id

View solution in original post

4 Replies
pklove
Community Champion

Are you doing this with an admin token?

"Note that typically users will only be able to upload files to their own files section".

So if doing this as an admin, add:

    ?as_user_id=user_id

Peter, thanks for your reply.

I have added the ?as_user_id=user_id to my POST call but I still get the Unauthorized issue.

First, my user account is an admin account.

Perhaps it has something to do with this sentence in the API documentation:

In order to masquerade via the API, the calling user must have the "Become other users" permission

I searched for this permission in our role "Account Admin" and I cannot find it. The only thing I find is one called "Users - act as" and all admin accounts already have it.

I found another side:

Canvas Developers 

where it says:

The workflows which leverage API can be further automated or simplified by obtaining an admin token with the “become other users” permission.

My questions are

Could this be why I'm getting the unauthorized response from the server?

Where do I set the "become other users" permission for an account in the UI in Canvas?

How do I obtain an admin token with this “become other users” permission.? I know I can generate an access token for my account but there is no option there for giving this permission.

Thanks

pklove
Community Champion

To see if the as_user_id is working for you or if it is another problem, try this simple example:

     /api/v1/users/self/profile?as_user_id=some_user

If masquerading is working you should be able to get the profile for some_user.

Peter,

Thanks for the tip in regards to as_user_id=some_user

The problem was just that in the some_user I was placing my user id but what I'm supposed to do is put the user I'm trying to masquerade in.. of course.