เอกสารอ้างอิง API
ZEPETO World Open API
การสร้างโทเค็นการตรวจสอบสิทธิ์ JWT เพื่อใช้กับ API เปิด
12 นาที
รูปแบบคำขอ api เปิด api เปิดของ zepeto ถูกเรียกผ่าน http หากมีเนื้อหาในคำขอ พารามิเตอร์จะต้องถูกส่งในรูปแบบ json ตัวอย่างประเภทเนื้อหาที่ถูกต้องแสดงด้านล่าง และอาจมีความแตกต่างเล็กน้อยขึ้นอยู่กับไลบรารีภาษาการเขียนโปรแกรมที่เกี่ยวข้อง content type application/json; charset=utf 8 รับคีย์เข้าถึง, คีย์ลับจาก zepeto studio ก่อนที่จะสร้างโทเค็นการตรวจสอบสิทธิ jwt คุณต้องรับคีย์เข้าถึงและคีย์ลับจากคอนโซล zepeto studio 📘 กรุณาอ้างอิงจากคู่มือต่อไปนี้ การจัดการ open api docid\ b994ljngjnqwjvqvmoc6w การสร้างโทเค็นการตรวจสอบสิทธิ jwt api เปิดของ zepeto สร้างโทเค็น jwt( https //jwt io https //jwt io ) ตามรูปแบบที่อิงจากคีย์เข้าถึงและคีย์ลับที่ออกให้สำหรับแต่ละคำขอและส่งในส่วนหัว authorization hs256 เป็นวิธีการลงนามที่แนะนำ และความลับที่จะใช้ในการลงนามคือคีย์ลับที่ออกให้ ข้อมูลใน jwt token มีรูปแบบดังต่อไปนี้ ข้อมูลใน jwt token { "access key" "issued access key (required)", "nonce" "randomized uuid value (required)", "uri hash" "a hashed value of the uri, including the query params, excluding the base path (required)", "body hash" "hashed value of the request body" } uri hash คือค่าที่ถูกแฮชของ uri รวมถึงพารามิเตอร์การค้นหา ยกเว้นเส้นทางหลัก body hash คือค่าที่ถูกแปลงเป็นสตริง json และถูกแฮชเพื่อแทรกลงใน payload เฉพาะเมื่อมีเนื้อหาการร้องขอ; จะถูกละเว้นหากไม่มีเนื้อหาการร้องขอ ในกรณีเช่นนี้ จะต้องไม่มีช่องว่างระหว่างคีย์และค่าของสตริง json uri hash และ body hash ต้องถูกแฮชให้มีค่าเหมือนกับพารามิเตอร์การค้นหาและเนื้อหาการร้องขอที่ส่งไปยังการร้องขอ (ลำดับของค่าต้องเหมือนกันด้วย) จำกัดจำนวนการเรียก api สูงสุด 300 ครั้งใน 1 นาที ตัวอย่างเมื่อไม่มีเนื้อหาคำขอ กรุณาใส่คีย์การเข้าถึง, คีย์ลับ, worldld, uri, และพารามิเตอร์การค้นหาตาม api ที่คุณต้องการใช้ โค้ดตัวอย่างด้านล่างนี้เขียนขึ้นตาม api ข้อมูลผู้เล่นของหมวดหมู่ datastorage 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}`; ตัวอย่างเมื่อมี request body กรุณาใส่ access key, secret key, worldid, uri, และ body param ตาม api ที่คุณต้องการใช้ โค้ดตัวอย่างด้านล่างนี้เขียนขึ้นจาก set player data api ของหมวด datastorage 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(); playerdata datamap = new playerdata("test", "test value"); 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 = "bearer " + jwttoken; python import jwt import uuid import hashlib import base64 import simplejson as json accesskey = 'accesskey' secretkey = 'secretkey' worldid = 'com test world' uri = '/datastorage/v1/worlds/' + worldid + '/player data' hash = hashlib sha256() hash update(uri encode()) param = { 'playerid' 'testplayerid', 'data' \[ { 'key' 'test', 'value' 'test 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 = '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'; 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 เป็นฟีเจอร์ที่ให้ใช้ในเว็บหรือแอปแยกต่างหาก ขณะนี้ สคริปต์เซิร์ฟเวอร์ zepeto ไม่สามารถเรียกใช้ zepeto open api ได้ หากคุณต้องการทำการเรียก open api ใน zepeto multiplayer เราขอแนะนำวิธีการดังต่อไปนี้ ตั้งค่าเซิร์ฟเวอร์แยกต่างหากเพื่อดำเนินการตรรกะทางธุรกิจที่จำเป็นโดยการสื่อสารกับ open api ใช้แพ็คเกจ httpservice ในเซิร์ฟเวอร์ zepeto เพื่อสื่อสารโดยตรงกับเซิร์ฟเวอร์ที่คุณตั้งค่าไว้ ดำเนินการวิธีการตรวจสอบสิทธิ์ที่ค่อนข้างง่ายระหว่างเซิร์ฟเวอร์ เช่น การใช้ http authorize headers เพื่อเปิดใช้งานการเรียกภายในฟีเจอร์ที่รองรับโดยเซิร์ฟเวอร์ zepeto