User Avatars and the canvas-api gem

195 views
Skip to first unread message

justi...@gmail.com

unread,
Dec 23, 2014, 4:32:27 PM12/23/14
to canvas-lms...@googlegroups.com
Has anyone tried adding Avatars to user via the API? I've experimented with doing it during create, but Canvas ignores the value. That doesn't surprise me since the API documentation doesn't indicate you can add an avatar when creating a user but I thought I'd try. The omission does seem odd.

I then tried adding an User Avatar via and edit. Here's the documentation on the method:
https://canvas.instructure.com/doc/api/users.html#method.users.update

It will work if I just use curl:
curl 'https://atomicjolt.instructure.com/api/v1/users/31?access_token=a_valid_token' \
  -X PUT \
  -F 'user[avatar][url]=https://drive.google.com/open?id=0B7IBzacZB31hU1FEVHRRZEVIZDg/edit'

However, I'm using the canvas-api gem that Brian Whitmer created:
https://github.com/whitmer/canvas-api

The code looks like this and fails every time returning a 100 http status code:

canvas_uri = 'http://canvas.instructure.com'
canvas_api_key = 'get_valid_key'
user_id = 60 # hard coded for simplicity
payload = { "user[avatar][url]" => "https://docs.google.com/file/d/0B7IBzacZB31hU1FEVHRRZEVIZDg/edit" }

canvas_api = Canvas::API.new(host: canvas_uri, token: canvas_api_key)

canvas_api.put("/api/v1/users/#{user_id}", payload)

Anyone have any idea how to get this to work?


Thanks,
Justin

chip.m...@gmail.com

unread,
Jun 24, 2016, 1:58:19 PM6/24/16
to Canvas LMS API Users
I have not been able to this with this API, but was able to do in using python:
import hashlib # to get the MD5 has functions
import urllib # to get the function for % encoding of the URL
...
baseUrl
= 'https://xxx.instructure.com/api/v1/' # changed to your domain
gravatar_base_url
='https://secure.gravatar.com/avatar/'

header
= {'Authorization' : 'Bearer ' + access_token}
payload
= {}
...
def insert_avatar_url(user_id, user_e_mail, url_to_avatar):
 url
= baseUrl + 'users/%s' %(user_id)

 hashed_user_email
=hashlib.md5(user_e_mail)
 constructed_url
=gravatar_base_url+hashed_user_email.hexdigest()+"?s=50&d="+urllib.quote(url_to_avatar, safe='')

 payload
= {'user[avatar][url]': constructed_url}
 
 r
= requests.put(url, headers = header, data = payload)

This successfully sets the URL as specified, which includes a default URL to the user's avatar (in case they do not actually have a gravatar avatar registered).
Chip
Reply all
Reply to author
Forward
0 new messages