概要
以下の記事で、Basic認証を使ったPythonによるコンテンツ登録を行いました。

Pythonを使ってDrupalにコンテンツを追加する
Pythonを使ってDrupalにコンテンツを追加する
今回は、以下の記事を参考に、API Key Authenticationを試しました。
Post to Drupal using JSON:API from a Vue.js Front-end
In this article, we will be using a Vue.js frontend to post to a Drupal 9 site using JSON:API.
API Key Authentication
以下のモジュールを使用しました。
Key auth
Provides simple key-based authentication on a per-user basis similar to basic_auth but without requiring usernames or passwords.
ユーザの編集画面に「Key authentication」というタブが表示され、APIキーを生成できました。

APIキーを使用する場合には、以下のようなプログラムで実行することができました。
import requests
endpoint = 'http://{ipアドレス or ドメイン名}/jsonapi/node/article'
key = '{APIキー}'
headers = {
'Accept': 'application/vnd.api+json',
'Content-Type': 'application/vnd.api+json',
"api-key": key
}
payload = {
"data": {
"type": "node--article",
"attributes": {
"title": "What's up from Python",
"body": {
"value": "Be water. My friends.",
"format": "plain_text"
}
}
}
}
r = requests.post(endpoint, headers=headers, json=payload)
r.json()
多言語対応における注意点
注意点として、翻訳データの作成はできないようでした。
Translations
JSON:API supports very simple multilingual functionality. It does not support advanced use cases. It currently relies on Drupal's default language negotiation mechanisms to negotiate the appropriate language of an entity and fall back when that translation is not available. In the long term, we intend to move away from this mechanism while retaining backwards compatibility; we want to add a JSON:API-spec compliant, formal translation support. The current language negotiation mechanism: works well for GET requests for individual resources, collection resources and includes.
作成済みの翻訳データの更新は可能ですが、翻訳データがないノードに対しては、以下のエラーが発生しました。
{
"jsonapi": {
"version": "1.0",
"meta": {
"links": {
"self": {
"href": "http://jsonapi.org/format/1.0/"
}
}
}
},
"errors": [
{
"title": "Method Not Allowed",
"status": "405",
"detail": "The requested translation of the resource object does not exist, instead modify one of the translations that do exist: ja."
}
]
}
この点について、既に対応策ができているかもしれません。引き続き調査したいと思います。
まとめ
DrupalのJSON:APIの利用にあたり、参考になりましたら幸いです。
動画版(生成AIによる自動生成): この記事の内容をずんだもん×四国めたんの掛け合いで解説しています。自動生成のため、内容に誤りがある可能性があります。正確な情報は記事本文をご参照ください。



コメント
…