API参考
ZEPETO World Open API
为开放API创建JWT身份验证令牌
12分
开放api请求格式 zepeto开放api通过http调用。 如果请求中存在主体,参数必须以json格式发送。 有效内容类型的示例如下,具体可能会根据各自的编程语言库略有不同。 content type application/json; charset=utf 8 从zepeto studio获取访问密钥和秘密密钥 在创建jwt身份验证令牌之前,您需要从zepeto studio控制台获取访问密钥和秘密密钥。 📘 请参考以下指南。 管理开放 api docid\ tevie5e km35x9z5oeamp 创建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调用数量限制:每分钟最多可进行300次调用。 没有请求体的示例 请根据您希望使用的api输入访问密钥、秘密密钥、世界id、uri和查询参数。 下面的示例代码是基于数据存储类别的获取玩家数据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输入访问密钥、秘密密钥、worldid、uri和body参数。 下面的示例代码是基于数据存储类别的设置玩家数据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(); 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 调用。 如果您想在 zepeto 多人游戏中进行 open api 调用,我们建议以下方法: 设置一个单独的服务器,通过与 open api 通信来执行必要的业务逻辑。 在 zepeto 服务器中使用 httpservice 包直接与您设置的服务器进行通信。 在服务器之间实现相对简单的身份验证方法,例如使用 http 授权头,以便在 zepeto 服务器支持的功能内进行调用。