やること
在庫取得
必要な情報
接続用のクライアントIDとシークレット - アカウントマネージャから作る?今回は与えられた状態から開始とする
接続先ドメイン
在庫リストID
商品ID
OCAPI設定
管理 > サイトの開発 > Open Commerce API 設定
タイプの選択:データ
範囲の選択:グローバル (組織全体)
{
"_v": "23.2", ← 呼び出し時のバージョンと違っていてもOK
"clients":
[
{
"client_id": "<クライアントID>",
"resources":
[
~ 中略 ~
{
"resource_id": "/inventory_lists/*/product_inventory_records/*",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
}
]
}
]
}
アクセストークン取得
curlコマンド
curl --trace-ascii - --ssl-no-revoke https://account.demandware.com/dwsso/oauth2/access_token?grant_type=client_credentials --data-urlencode "client_id=<クライアントID>" --data-urlencode "client_secret=<クライアントシークレット>"
正常実行結果(抜粋)
POST /dwsso/oauth2/access_token?grant_type=client_credentials HTTP/1.1
Host: account.demandware.com
Content-Type: application/x-www-form-urlencoded
client_id=<クライアントID>&client_secret=<クライアントシークレット>
HTTP/1.1 200 OK
content-type: application/json;charset=UTF-8
{"access_token":"<アクセストークン>",~略~}
在庫取得
curlコマンド
curl -v --ssl-no-revoke https://<接続先ドメイン>/s/-/dw/data/v23_2/inventory_lists/<在庫リストID>/product_inventory_records/<商品ID> --header "Authorization: Bearer <アクセストークン>"
正常実行結果(抜粋)
GET /s/-/dw/data/v23_2/inventory_lists/<在庫リストID>/product_inventory_records/<商品ID> HTTP/1.1
Host: <接続先ドメイン>
Authorization: Bearer <アクセストークン>
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
{"_v":"23.2","_type":"product_inventory_record","_resource_state":"~略~","allocation":{"_type":"product_inventory_record_allocation","amount":10,"reset_date":"2024-12-26T09:47:01.802Z"},"ats":10,"creation_date":"2024-05-31T02:00:03.000Z","inventory_list_id":"<在庫リストID>","inventory_turnover":0,"last_modified":"2024-12-26T09:47:01.000Z","link":"~略~","perpetual_flag":false,"pre_order_back_order_handling":"none","product_id":"<商品ID>","product_name":"~略(商品名)~","quantity_on_order":0,"stock_level":10}
異常実行結果(抜粋)
アクセストークン不正(例えば期限切れ等)
HTTP/1.1 401
{"_v":"23.2","fault":{"arguments":{"accessToken":"<アクセストークン>"},"type":"InvalidAccessTokenException","message":"Unauthorized request! The access token '<アクセストークン>' is invalid."}}
OCAPI設定不足(例えばmethodsにgetが不足している等)
HTTP/1.1 403 Forbidden
{"_v":"23.2","fault":{"arguments":{"method":"GET","path":"~略~"},"type":"ClientAccessForbiddenException","message":"Access to resource 'GET /data/v23_2/inventory_lists/<在庫ID>/product_inventory_records/<商品ID>' isn't allowed for the current client."}}
OCAPI設定を修正後にトークン取得からやり直し