# fetch를 통해 API에 접근할 때 API 키로 인증하기

**URL:** <https://meta.discourse.org/t/authenticating-using-api-keys-when-accessing-api-through-fetch/134149>\
**Category:** Development\
**Tags:** rest-api\
**Created:** [11월 22, 2019, 12:32오후 UTC](https://meta.discourse.org/t/authenticating-using-api-keys-when-accessing-api-through-fetch/134149 "2019-11-22T12:32:59Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Lucas\_Meijer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lucas_meijer/32/157389_2.png) [@Lucas\_Meijer](https://meta.discourse.org/u/Lucas_Meijer)\
**Post date:** [11월 22, 2019, 12:32오후 UTC](https://meta.discourse.org/t/authenticating-using-api-keys-when-accessing-api-through-fetch/134149/1 "2019-11-22T12:32:59Z")

</div>

안녕하세요!

저는 커뮤니티를 Discourse에 두는 매거진을 위해 헤드리스 WP 사이트를 구축하고 있으며, 댓글을 아티클 페이지에 통합할 예정입니다. API 문서를 찾아보고 정말 기뻤는데, 사이트에는 다크 모드와 라이트 모드가 모두 지원되므로 iframe 임베드는 제가 원하던 방식이 아니었기 때문입니다. 댓글 템플릿이 루트 요소에서 CSS 변수를 상속받아야 합니다.

하지만 API에 직접 접근하려고 할 때([https://discourseurl.com/t/{id}.json](https://discourseurl.com/t/%7Bid%7D.json) 에 fetch 사용) 계속 레이트 리밋에 부딪히고 있습니다. 그래서 API 키를 추가하고 이를 사용하여 인증을 시도해 보았습니다.

아래 코드를 사용하고 있습니다:

```plaintext
fetch(this.apiUrl, {
        headers: {
          'User-Api-Key': '{Discourse 관리자에서 가져온 사용자 API 키}',
        },
      })

```

어떤 키를 사용하든(관리자 키로 시도해 보기도 했습니다) 다음과 같은 오류가 발생합니다:

````plaintext
error_type: "invalid_access"
errors: Array ["Du har inte behörighet att visa den efterfrågade resursen."]```

````

대략적인 번역: “요청한 리소스에 접근할 권한이 없습니다.”

API 키 작동 방식에 대해 제가 놓치고 있는 부분이 있을까요? 레이트 리밋에 걸리지 않고 이 API 엔드포인트를 요청하는 권장 접근 방식이 무엇일까요?

---

_[View the full topic](https://meta.discourse.org/t/authenticating-using-api-keys-when-accessing-api-through-fetch/134149)._
