Setting your avatar

Each user has a profile. Within this profile is the URL to an image that is used as this user's avatar.

You can access the user's profile (in this case using Ruby) with:

 

canvas.get("/api/v1/users/self/profile")
{
                "id" => 29,
              "name" => "Gerald Q. Maguire Jr. temp",
        "short_name" => "Gerald Q. Maguire Jr. temp",
     "sortable_name" => "te, Gerald Q. Maguire Jr.",
        "avatar_url" => "https://secure.gravatar.com/avatar/52e6e0ae2260166c91cd528ba0c72263?s=50&d=https%3A%2F%2Fkth.instructure.com%2Fimages%2Fmessages%2Favatar-50.png",
             "title" => nil,
               "bio" => nil,
     "primary_email" => "maguire@kth.se",
          "login_id" => "maguire@kth.se",
    "integration_id" => nil,
         "time_zone" => "Europe/Stockholm",
            "locale" => nil,
          "calendar" => {
        "ics" => "https://kth.instructure.com/feeds/calendars/user_D3eZRBEctTqK7Yhd849duUymMQG6Nr1XvL99HTLs.ics"
    }
}

As can be seen in the line:

 "avatar_url" => "https://secure.gravatar.com/avatar/52e6e0ae2260166c91cd528ba0c72263?s=50&d=https%3A%2F%2Fkth.instructure.com%2Fimages%2Fmessages%2Favatar-50.png",

we see that the URL has the form:

https://secure.gravatar.com/avatar                                       base URL
52e6e0ae2260166c91cd528ba0c72263                                          MD5 has of user's email-address
?s=50                                                                     size of image in pixels
&d=https%3A%2F%2Fkth.instructure.com%2Fimages%2Fmessages%2Favatar-50.png  URL to default image

From the base URL we can see that the URL points to Gravatar Links to an external site.. Their service is providing avatar images that any service can use, thus each user has a consistent avatar across these applications. You can create an avatar and register it with their service is you have  an account (which requires that you have a WordPress.com account), see: http://en.gravatar.com/support/how-to-sign-up Links to an external site. 

The MD5 has of your e-mail address is used as your account ID. You can compute this hash, for example in Rudy as:

> require 'digest/md5' 
true
> Digest::MD5.hexdigest("maguire@kth.se")
"52e6e0ae2260166c91cd528ba0c72263"

You can set the size of the avatar using the "s=" parameter

What happens if you do not have an avatar at Gravatar? This is why you can specify a default image to be used with the "d=" parameter. According to Canvas the image should be 128x128, but it does not have to be. However, you have to encode the URL to the image (in Ruby):

> CGI.escape("http://people.kth.se/~maguire/maguire-march-2004-s.jpg")
"http%3A%2F%2Fpeople.kth.se%2F%7Emaguire%2Fmaguire-march-2004-s.jpg"

You can set this image as your avatar using the following command line:

curl 'https://kth.instructure.com/api/v1/users/29.json' \
 -X PUT \
 -F 'user[avatar][url]=https://secure.gravatar.com/avatar/52e6e0ae2260166c91cd528ba0c72263?s=50&d=http%3A%2F%2Fpeople.kth.se%2F%7Emaguire%2Fmaguire-march-2004-sq.jpg' \
 -H "Authorization: Bearer 8..K"
> > >
{"id":29,"name":"Gerald Q. Maguire Jr. temp",
 "sortable_name":"te, Gerald Q. Maguire Jr.",
 "short_name":"Gerald Q. Maguire Jr. temp",
 "avatar_url":"https://secure.gravatar.com/avatar/52e6e0ae2260166c91cd528ba0c72263?s=50\u0026d=http%3A%2F%2Fpeople.kth.se%2F%7Emaguire%2Fmaguire-march-2004-sq.jpg",
 "locale":null,
 "time_zone":"Stockholm"}

You can restore the previous URL by saying:

 curl 'https://kth.instructure.com/api/v1/users/29.json' \
     -X PUT \
     -F 'user[avatar][url]=https://secure.gravatar.com/avatar/52e6e0ae2260166c91cd528ba0c72263?s=50&d=https%3A%2F%2Fkth.instructure.com%2Fimages%2Fmessages%2Favatar-50.png' \
     -H "Authorization: Bearer 8779~lXFT80ZfNaifMmiRQG89G37GsJB5plP1ujVfqk85VmyKc3RKYIYeZLblbl8dbUgK"
> > > 
{"id":29,"name":"Gerald Q. Maguire Jr. temp",
  "sortable_name":"te, Gerald Q. Maguire Jr.",
  "short_name":"Gerald Q. Maguire Jr. temp",
  "avatar_url":"https://secure.gravatar.com/avatar/52e6e0ae2260166c91cd528ba0c72263?s=50\u0026d=https%3A%2F%2Fkth.instructure.com%2Fimages%2Fmessages%2Favatar-50.png",
  "locale":null,
  "time_zone":"Stockholm"}

However, if you do not like your picture, but do want something that is consistent and unique you can use one of the default images that Gravatar can provide, in the example below I have used an

curl 'https://kth.instructure.com/api/v1/users/29.json' \
     -X PUT \
     -F 'user[avatar][url]=https://secure.gravatar.com/avatar/52e6e0ae2260166c91cd528ba0c72263?s=50&d=https://www.gravatar.com/avatar/00000000000000000000000000000000?s=50&d=identicon' \
     -H "Authorization: Bearer 8..K"

The identicon is based upon the MD5 hash, in this case it is

identicon-00000000000000000000000000000000.png

While for the md5 has for my e-mail (52e6e0ae2260166c91cd528ba0c72263) the identicon is:

chip-identicon-52e6e0ae2260166c91cd528ba0c72263.png

The resulting user avatar is shown for the account as:

avatar_set_chip_picture-20160624-identicon-cropped.png

If you have problems updating your avatar, check that it is changeable by the user:

canvas.get("/api/v1/users/29")
{
               "id" => 29,
             "name" => "Gerald Q. Maguire Jr. temp",
    "sortable_name" => "te, Gerald Q. Maguire Jr.",
       "short_name" => "Gerald Q. Maguire Jr. temp",
       "avatar_url" => "https://secure.gravatar.com/avatar/52e6e0ae2260166c91cd528ba0c72263?s=50&d=https%3A%2F%2Fkth.instructure.com%2Fimages%2Fmessages%2Favatar-50.png",
           "locale" => nil,
      "permissions" => {
          "can_update_name" => false,
        "can_update_avatar" => true
    }
}

The attribute "permissions" indicates that in this above case the user cannot update their name, but can update their avatar.

You can also use a program to change the avatar. Run the sample program ( Download source

):

./set_avatar.py  29 "maguire@kth.se" "http://people.kth.se/~maguire/maguire-march-2004-sq.jpg"
Outputs:
u'{"id":29,"name":"Gerald Q. Maguire Jr. temp","sortable_name":"te, Gerald Q. Maguire Jr.",
  "short_name":"Gerald Q. Maguire Jr. temp",
  "avatar_url":"https://secure.gravatar.com/avatar/52e6e0ae2260166c91cd528ba0c72263?s=50\\u0026d=http%3A%2F%2Fpeople.kth.se%2F%7Emaguire%2Fmaguire-march-2004-sq.jpg",
  "locale":null,
  "time_zone":"Stockholm"}'

The result is:

avatar_set_chip_picture-20160624-cropped.png

The essence of the python program is:

import hashlib        # to get the MD5 has functions
import urllib            # to get the function for % encoding of the URL
...
baseUrl = 'https://kth.instructure.com/api/v1' # changed to KTH 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)

Taking the image URL from the user's KTH profile via a program ( Download source

).

 ./person.py -v 29 maguire
ARGV      : ['-v', '29', 'maguire']
VERBOSE   : True
INPUT    : default.mods
REMAINING : ['29', 'maguire']
https://www.kth.se/profile/maguire
status code: 
200
xpath_url_to_image: 
person.image https://www.kth.se/social/files/576d7ae3f2765459470e7b0e/chip-identicon-52e6e0ae2260166c91cd528ba0c72263_200.png
person.email maguire@kth.se
https://kth.instructure.com/api/v1/users/29
{'user[avatar][url]': 'https://secure.gravatar.com/avatar/52e6e0ae2260166c91cd528ba0c72263?s=50&d=https%3A//www.kth.se/social/files/576d7ae3f2765459470e7b0e/chip-identicon-52e6e0ae2260166c91cd528ba0c72263_200.png'}

Note that the reason that the user's Canvas ID has to be input, is because I do not have administrative access to canvas, hence I cannot make a list all of the user accounts and then match on user name or e-mail address:

curl -H "Authorization: Bearer 8..K" "https://kth.instructure.com/api/v1/site_admin/users"
{"status":"unauthorized","errors":[{"message":"user not authorized to perform that action"}]}