pablogg
(Pablo)
2020 年 4 月 12 日午後 4:51
1
昨日 Discourse をアップグレードしたところ、すべての API コールバックでエラーが発生しました。問題の原因は認証ヘッダー (1) であることがわかりましたが、いくつかの呼び出しを試みると、以下のエラーが表示されます。
Access to XMLHttpRequest at 'https://mydomain.com/notifications.json?username=admin' from origin 'https://mydomain.com' has been blocked by CORS policy: Request header field api-username is not allowed by Access-Control-Allow-Headers in preflight response.
Vue を使用しており、ヘッダーに api-key と api-username を以下のように追加しました。
Vue.http
.get(
"https://discourse.mydomain.com/notifications.json?username="+input.username,
{
headers: {
"Api-Key":"xxxxxxxxxxxxxxxxx",
"Api-Username": "system",
"content-type": "application/json",
accept: "application/json"
}
}
)
(1) Discourse REST API Documentation
blake
(Blake Erickson)
2020 年 4 月 12 日午後 5:29
2
サイト設定で CORS の設定が必要かもしれません。
pablogg
(Pablo)
2020 年 4 月 12 日午後 5:54
3
はい、Discourse の設定で API を呼び出すドメインを追加しましたが、api-username フィールドに関するエラーがいつ解決すべきものか分かりません。
RGJ
(Richard - Communiteq)
2020 年 4 月 12 日午後 6:05
4
system の API キーをクライアントサイドのコードに記述しているんですか?
これでは、誰でもあなたの JavaScript コードからそのキーを抜き取って、あなたのフォーラムを完全に乗っ取られてしまいます。
Discourse への Ajax HTTP リクエストは、既存のセッションを利用するか、そもそもセッションを必要としないようにする必要があります。
「いいね!」 3
pablogg
(Pablo)
2020 年 4 月 12 日午後 6:09
5
単なるテストではなく、以下のコードでユーザー名を使用しています。
Vue.http
.get(
"https://discourse.mydomain.com/notifications.json?username="+input.username,
{
headers: {
"Api-Key":"xxxxxxxxxxxxxxxxxxxxxx",
"Api-Username": input.username
}
}
)
しかし、CORS と api-username フィールドに関連して同じエラーが発生しています。
「いいね!」 1
blake
(Blake Erickson)
2020 年 4 月 12 日午後 6:24
7
CORS リクエストでこれらの API 認証情報を使用するのは避けるべきです。そのため、そのヘッダーフィールドは許可されていません。
Oh, so you are trying to make this request from a client-side javascript application? That is not permitted for numerous reasons. For example, if you include an admin API key in the source code of your javascript app, any user could gain full admin access to your forum. Here is a previous topic on the subject.
ただし、CORS においては user-api ヘッダーは許可されています。
I’m trying to post comments to my Discourse instance from another web application using user api keys. I managed to get the user key following the method described in your original topic and this other useful discussion .
After getting the key, when I send it along with my requests as a value of the User-Api-Key header, I get the Request header field User-Api-Key is not allowed by Access-Control-Allow-Headers error.
As I asked in my PR , I’m not sure if it’s enough to add the headers in the file…
「いいね!」 3
pablogg
(Pablo)
2020 年 4 月 12 日午後 7:14
8
User API keys specification を読みました。
私の場合、JavaScript のフロントエンドアプリで SSO を使用しています。すべてのユーザーに対して認証 UI を使用せずに API を呼び出すことは可能でしょうか?api-username を使用できる方法があればよいのですが、可能でしょうか?
よろしくお願いいたします
pablogg
(Pablo)
2020 年 7 月 22 日午前 7:27
10
結局、API 統合をすべて変更する必要がなくなるよう、Discourse の以前のバージョンに戻しました。