Ruby gem API POST errors (update_avatar)

(Ron) #1


I’m having some trouble working with the API. Specifically the client.update_avatar command.

Code kinda looks like this:

client ="#{APIHOST}")
client.api_key = APIKEY
client.api_username = APIUSER
puts client.categories

and that all works

a few lines later, this statement does not work:

puts client.update_avatar(username: myuser, url: "https://someurl")

The failure I’m getting is:

/var/lib/gems/2.3.0/gems/discourse_api-0.10.1/lib/discourse_api/client.rb:79:in `post': {"failed"=>"FAILED"} (DiscourseApi::Error)
        from /var/lib/gems/2.3.0/gems/discourse_api-0.10.1/lib/discourse_api/api/users.rb:24:in `update_avatar'
        from ./forum_auto_permissions.rb:96:in `block in <main>'
        from /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/lib/nokogiri/xml/node_set.rb:187:in `block in each'
        from /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/lib/nokogiri/xml/node_set.rb:186:in `upto'
        from /var/lib/gems/2.3.0/gems/nokogiri-1.6.8/lib/nokogiri/xml/node_set.rb:186:in `each'
        from ./forum_auto_permissions.rb:82:in `<main>'

I’m a bit stuck. This piece of code worked last Oct/Nov when I did a proof of concept on the software. Any help would be appreciated.


ps. I’m using the generic Oauth2 plugin if that matters. It works perfectly.

(Matt Palmer) #2

There’s two situations in which avatar upload can fail, based on a quick glance at the code:

if SiteSetting.sso_overrides_avatar
  return render json: failed_json, status: 422
if !SiteSetting.allow_uploaded_avatars
  if type == "uploaded" || type == "custom"
    return render json: failed_json, status: 422

So, if the site has either of those settings set appropriately, you’ll get that failure.

(Ron) #3

I will check that out and let you know. I thought SSO and Oauth2 plugin where independent, but I will try!


(Ron) #4

Oddly enough my code worked as expected once I restarted the container.

Thanks all