Get /users/by-external/ throwing URL Not Found error after update

So i’ve been updating my forums from 1.6 to 3.1 which has involved moving to a new server and restoring backup then plugging everything back together

We use SSO for signing into both which still works

However discourse now seems to throw errors in some random areas that is causing a problem with account creation from the website using the API.
We were using the ruby gem discourse_api at version 0.10.1 which i updated to 0.48.1

The error comes at
DiscourseApiService.new.user(self)

which calls
resp = client.get("/users/by-external/#{user.id}")

which throws the error
{"errors"=>["The requested URL or resource could not be found."], "error_type"=>"not_found"}

This used to work fine but now for some reason the URL cannot be found.

I tried downgrading the discourse_api gem to 0.10.1 again which is what we were running previously and it gets past the client.get stage ok but instead fails at the next call with the same error

client.sync_sso(

{"errors"=>["The requested URL or resource could not be found."], "error_type"=>"not_found"}

The endpoint changed to /u/by-external/{external_id}.json at some point. And an API key and username is required for that endpoint

1 Like

i was actually able to solve it

Seems they updated the code to actually throw an error if no user is found so i had to rescue instead of compare if nil?

begin
    resp = client.get("/users/by-external/#{user.id}")
rescue DiscourseApi::NotFoundError => error
    puts error.response.body['errors'].first
    sync_user(user)
else
    resp.response.env.body['user']
end

also the response has changed from
resp['body']['user']
to
resp.response.env.body['user']

my last remaining problem is that when i call client.sync_sso i get a Internal server error 500 now

The logs show
ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "user_emails_pkey"

However, if i use the exact same details in the rails terminal and run client.sync_sso it runs perfectly fine

Just FYI i didn’t change the endpoint for the above parts to start working again so it seems the /users/by-external/ is still valid

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.