API リファレンス
ZEPETO World Open API
オープンAPIで使用するためのJWT認証トークンの作成
12min
オープンapiリクエストフォーマット zepetoオープンapiはhttp経由で呼び出されます。 リクエストにボディが含まれている場合、パラメータはjson形式で送信する必要があります。 有効なコンテンツタイプの例は以下に示されており、各プログラミング言語のライブラリによって若干の違いがある場合があります。 content type application/json; charset=utf 8 zepeto studioからアクセスキーとシークレットキーを取得する jwt認証トークンを作成する前に、zepeto studioコンソールからアクセスキーとシークレットキーを取得する必要があります。 📘 以下のガイドを参照してください。 オープンapiの管理 docid\ vvv0uxsyuydaghxufjqtm jwt認証トークンの作成 zepetoオープンapiは、各リクエストに発行されたアクセスキーとシークレットキーに基づいてjwt( https //jwt io https //jwt io )形式のトークンを生成し、authorizationヘッダーに送信します。 署名方法としてhs256が推奨されており、署名に使用する秘密は発行されたシークレットキーです。 jwtトークンのペイロードは次の形式です: jwtトークンペイロード { "access key" "発行されたアクセスキー(必須)", "nonce" "ランダム化されたuuid値(必須)", "uri hash" "クエリパラメータを含むuriのハッシュ値、ベースパスは除外(必須)", "body hash" "リクエストボディのハッシュ値" } uri hashは、ベースパスを除くクエリパラメータを含むuriのハッシュ値です。 body hashは、リクエストボディが存在する場合にのみペイロードに挿入されるように変換されたjson文字列の値であり、リクエストボディが存在しない場合は省略されます。 その場合、json文字列のキーと値の間にスペースがあってはなりません。 uri hashとbody hashは、リクエストに送信されたクエリパラメータとリクエストボディと同じ値にハッシュ化されなければなりません。(値の順序も同じでなければなりません。) apiコールの回数制限:1分間に最大300回のコールが可能です。 リクエストボディがない場合の例 使用したいapiに従って、アクセスキー、シークレットキー、ワールドid、uri、およびクエリパラメータを入力してください。 以下の例コードは、datastorageカテゴリのget player data apiに基づいて記述されています。 java string accesskey = "accesskey"; string secretkey = "secretkey"; string worldid = "com test world"; string uri = "/datastorage/v1/worlds/" + worldid + "/player data"; messagedigest urihash = messagedigest getinstance("sha 256"); urihash update(uri getbytes(standardcharsets utf 8)); byte\[] urihashbytes = urihash digest(); objectmapper objectmapper = new objectmapper(); map\<string, object> payload = new hashmap<>(); payload put("access key", accesskey); payload put("nonce", uuid randomuuid() tostring()); payload put("uri hash", new string(base64 encodebase64(urihashbytes), standardcharsets utf 8)); string jwttoken = jwts builder() setpayload(objectmapper writevalueasstring(payload)) signwith(signaturealgorithm hs256, secretkey getbytes(standardcharsets utf 8)) compact(); string authorization = "bearer " + jwttoken; python import jwt import uuid import hashlib import base64 accesskey = 'accesskey' secretkey = 'secretkey' worldid = 'com test world' uri = '/datastorage/v1/worlds/' + worldid + '/player data?playerid=testplayerid\&keys=test' hash = hashlib sha256() hash update(uri encode()) payload = { 'access key' accesskey, 'nonce' str(uuid uuid4()), 'uri hash' base64 b64encode(hash digest()) decode('utf8') } jwt token = jwt encode(payload, secretkey) authorization = 'bearer {}' format(jwt token) nodejs import as jwt from 'jsonwebtoken'; import as uuid from 'uuid'; import as crypto from 'crypto js'; import { buffer } from 'safe buffer'; const accesskey = 'accesskey'; const secretkey = 'secretkey'; const worldid = 'com test world'; const uri = '/datastorage/v1/worlds/' + worldid + '/player data?playerid=testplayerid\&keys=test'; const hash = crypto sha256(uri); const payload = { access key accesskey, nonce uuid v4(), uri hash buffer from(hash tostring(), 'hex') tostring('base64') }; const jwttoken = jwt sign(payload, secretkey); const authorization = `bearer ${jwttoken}`; リクエストボディがある場合の例 使用したいapiに応じて、アクセスキー、シークレットキー、ワールドid、uri、およびボディパラメータを入力してください。 以下の例コードは、datastorageカテゴリのset player data apiに基づいて記述されています。 java string accesskey = "アクセスキー"; string secretkey = "シークレットキー"; string worldid = "com test world"; string uri = "/datastorage/v1/worlds/" + worldid + "/player data"; messagedigest urihash = messagedigest getinstance("sha 256"); urihash update(uri getbytes(standardcharsets utf 8)); byte\[] urihashbytes = urihash digest(); objectmapper objectmapper = new objectmapper(); playerdata datamap = new playerdata("テスト", "テスト値"); list\<playerdata> datalist = new arraylist<>(); datalist add(datamap); playerdatasetparam param = new playerdatasetparam(datalist, "testplayerid"); messagedigest paramhash = messagedigest getinstance("sha 256"); paramhash update(objectmapper writevalueasstring(param) getbytes(standardcharsets utf 8)); byte\[] paramhashbytes = paramhash digest(); map\<string, object> payload = new hashmap<>(); payload put("access key", accesskey); payload put("nonce", uuid randomuuid() tostring()); payload put("uri hash", new string(base64 encodebase64(urihashbytes), standardcharsets utf 8)); payload put("body hash", new string(base64 encodebase64(paramhashbytes), standardcharsets utf 8)); string jwttoken = jwts builder() setpayload(objectmapper writevalueasstring(payload)) signwith(signaturealgorithm hs256, secretkey getbytes(standardcharsets utf 8)) compact(); string authorization = "ベアラー " + jwttoken; python import jwt import uuid import hashlib import base64 import simplejson as json accesskey = 'アクセスキー' secretkey = 'シークレットキー' worldid = 'com test world' uri = '/datastorage/v1/worlds/' + worldid + '/player data' hash = hashlib sha256() hash update(uri encode()) param = { 'playerid' 'testplayerid', 'data' \[ { 'key' 'テスト', 'value' 'テスト値' } ] } param hash = hashlib sha256() param hash update(json dumps(param, ensure ascii=false, encoding='surrogatepass') encode()) payload = { 'access key' accesskey, 'nonce' str(uuid uuid4()), 'uri hash' base64 b64encode(hash digest()) decode('utf8'), 'body hash' base64 b64encode(param hash digest()) decode('utf8') } jwt token = jwt encode(payload, secretkey) authorization = 'ベアラー {}' format(jwt token) nodejs import as jwt from 'jsonwebtoken'; import as uuid from 'uuid'; import as crypto from 'crypto js'; import { buffer } from 'safe buffer'; const accesskey = 'accesskey'; const secretkey = 'secretkey'; const worldid = 'com test world'; const uri = '/datastorage/v1/worlds/' + worldid + '/player data'; const hash = crypto sha256(uri); const param = { playerid 'testplayerid', data \[ { value 'test value', key 'test' } ] }; const paramhash = crypto sha256(json stringify(param,null,0)); const payload = { access key accesskey, nonce uuid v4(), uri hash buffer from(hash tostring(), 'hex') tostring('base64'), body hash buffer from(paramhash tostring(), 'hex') tostring('base64') }; const jwttoken = jwt sign(payload, secretkey); const authorization = `bearer ${jwttoken}`; ❗️ 注意 openapiは、別のwebまたはアプリで使用するために提供される機能です。 現在、zepetoサーバースクリプトはzepeto open api呼び出しを行うことができません。 zepetoマルチプレイヤーでopen api呼び出しを行いたい場合、以下の方法をお勧めします: open apiと通信して必要なビジネスロジックを実行するために、別のサーバーを設定します。 zepetoサーバー内で直接通信するために、設定したサーバーと通信するためにhttpserviceパッケージを使用します。 http認証ヘッダーを使用するなど、サーバー間で比較的簡単な認証方法を実装して、zepetoサーバーによってサポートされている機能内での呼び出しを可能にします。