# 업데이트 후 Get /users/by-external/ 요청 시 URL Not Found 오류 발생

**URL:** https://meta.discourse.org/t/get-users-by-external-throwing-url-not-found-error-after-update/252410
**Category:** SSO
**Tags:** rest-api
**Created:** [1월 20, 2023, 12:18오후 UTC](https://meta.discourse.org/t/get-users-by-external-throwing-url-not-found-error-after-update/252410 "2023-01-20T12:18:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bobpies](https://avatars.discourse-cdn.com/v4/letter/b/3ec8ea/32.png) [@bobpies](https://meta.discourse.org/u/bobpies)
#### Post date: [1월 20, 2023, 12:18오후 UTC](https://meta.discourse.org/t/get-users-by-external-throwing-url-not-found-error-after-update/252410/1 "2023-01-20T12:18:55Z")

</div>

1.6에서 3.1로 포럼을 업데이트하는 작업을 진행 중인데, 새 서버로 이전하고 백업을 복원한 뒤 모든 것을 다시 연결하는 과정이 포함되어 있습니다.

양쪽 모두에 SSO를 사용하여 로그인하는데, 이 부분은 여전히 정상적으로 작동합니다.

그러나 Discourse에서 일부 랜덤한 영역에서 오류가 발생하고 있으며, 이로 인해 웹사이트에서 API를 사용하여 계정을 생성하는 데 문제가 생기고 있습니다.

이전에는 0.10.1 버전의 ruby gem `discourse_api`를 사용했는데, 이를 0.48.1로 업데이트했습니다.

오류는 다음 지점에서 발생합니다:  
`DiscourseApiService.new.user(self)`

이것은 다음을 호출합니다:  
`resp = client.get("/users/by-external/#{user.id}")`

그리고 다음과 같은 오류가 발생합니다:  
`{"errors"=>["The requested URL or resource could not be found."], "error_type"=>"not_found"}`

이전에는 정상적으로 작동했는데, 이제 어떤 이유에서인지 URL을 찾을 수 없다고 합니다.

이전에 사용했던 0.10.1 버전으로 `discourse_api` gem을 다시 다운그레이드해 보았는데, `client.get` 단계는 정상적으로 통과하지만, 대신 다음 호출에서 동일한 오류가 발생합니다.

`client.sync_sso(`

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

---

<div class="post-metadata">

### Author: ![not-ethan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/not-ethan/32/282617_2.png) [@not-ethan](https://meta.discourse.org/u/not-ethan)
#### Post date: [1월 20, 2023, 3:40오후 UTC](https://meta.discourse.org/t/get-users-by-external-throwing-url-not-found-error-after-update/252410/2 "2023-01-20T15:40:23Z")

</div>

어느 시점에 엔드포인트가 `/u/by-external/{external_id}.json`으로 변경되었습니다. 해당 엔드포인트에는 API 키와 사용자 이름이 필요합니다.

---

<div class="post-metadata">

### Author: ![bobpies](https://avatars.discourse-cdn.com/v4/letter/b/3ec8ea/32.png) [@bobpies](https://meta.discourse.org/u/bobpies)
#### Post date: [1월 20, 2023, 3:54오후 UTC](https://meta.discourse.org/t/get-users-by-external-throwing-url-not-found-error-after-update/252410/3 "2023-01-20T15:54:53Z")

</div>

사실 문제를 해결했습니다.

사용자를 찾을 수 없으면 실제로 에러를 발생시키도록 코드가 업데이트된 것 같습니다. 그래서 nil?과 비교하는 대신 rescue를 사용해야 했습니다.

```plaintext
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

```

또한 응답 형식이  
`resp['body']['user']`  
에서  
`resp.response.env.body['user']`  
로 변경되었습니다.

남은 마지막 문제는 **client.sync\_sso** 를 호출할 때 이제 Internal server error 500이 발생한다는 것입니다.

로그에는 다음과 같은 내용이 표시됩니다.  
`ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "user_emails_pkey"`

하지만 rails 터미널에서 **완전히 동일한** 세부 정보를 사용하여 client.sync\_sso를 실행하면 완벽하게 정상적으로 동작합니다.

참고로 위 부분이 다시 작동하도록 엔드포인트를 변경하지 않았으므로 /users/by-external/이 여전히 유효한 것 같습니다.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2월 19, 2023, 3:55오후 UTC](https://meta.discourse.org/t/get-users-by-external-throwing-url-not-found-error-after-update/252410/4 "2023-02-19T15:55:13Z")

</div>

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