تسمح مفاتيح API بدمج Discourse مع أنظمة أخرى أو أتمتة إجراءات مختلفة. ومع ذلك، فإن القوة العظيمة تحمل مسؤولية عظيمة. فإذا تمكن طرف خبيث من الوصول إلى مفتاح API، فقد يتمكن من الوصول إلى بيانات حساسة أو إجراء تغييرات على موقعك. وللتخفيف من ذلك وإضافة طبقة أمان إضافية، يمكنك الآن تقييد ما يمكن لمفتاح API القيام به باستخدام النطاقات (Scopes).
النطاقات والمعلمات المسموح بها
عند إنشاء مفتاح API جديد، يمكنك اختيار النطاقات التي تريدها. وإذا مررت المؤشر فوق أيقونة ، فستظهر لك وصفًا موجزًا لما تفعله. أما زر فسيظهر لك عناوين URL المرتبطة بذلك النطاق.
اختياريًا، يمكنك أيضًا تحديد المعلمات المقبولة. استخدم الفواصل لفصل القيم المتعددة.
ترحيل المفاتيح الحالية
عند إضافة هذه الميزة، تحولت المفاتيح الحالية إلى مفاتيح “عالمية”. ولترحيلها إلى نطاقات، يتعين عليك إلغاؤها وإنشاء مفاتيح جديدة.
إضافة نطاقات مخصصة
يمكن للإضافات إضافة نطاقات مخصصة عن طريق استدعاء دالة add_api_key_scope:
resource هو رمز يُستخدم لتجميع النطاقات ذات الصلة، بينما action هو كائن يحتوي على السمات التالية:
actions: قائمة تحتوي على إجراءات وحدة التحكم المسموح بها. الصيغة هي controller_name#method_name.
methods: قائمة بطرق HTTP المسموح بها (مثل %i[get]). استخدم هذا بدلاً من actions للمطابقة بناءً على طريقة HTTP بدلاً من إجراء وحدة التحكم.
params: قائمة تحتوي على أسماء المعلمات المسموح بها.
aliases: كائن يحتوي على اسم مختلف لوسيط مسموح به.
إذا أردت معرفة كيفية تعريف النطاقات الأساسية، راجع:
where I have read & read_list permissions on topics for the API key. /top.json works with the same key for instance. The endpoint works when I use a ‘Global Key’
I’d like to put the API key in my client for reading the list of categories and topics, so important to have a read only key!
Unfortunately, we only include a handful of scopes out of the box due to the high number of available endpoints. We may add new ones in the future, but in the meantime, you’ll have to extend them to suit your needs.
If you use a scoped API Key, calling endpoints not included by the scopes you choose won’t work. You can click the button to see which URLs are accepted.
This only works for public sites. Also, you won’t see things like private categories unless you are logged in and have enough permissions.
One thing I’d be interested in using our site’s API for would be for another site to check if a user exists with an email address and add to a group if so. I trust the site (another site within our parent organisation) but tightening down access options to only what’s needed seems wise if it were an option.