API REFERENCES
ZEPETO World Open API

오픈 API 사용을 위한 JWT 인증 토큰 만들기

13min
오픈 api 요청 형식 zepeto 오픈 api는 http를 통해 호출됩니다 요청에 본문이 포함된 경우, 매개변수는 json 형식으로 전송해야 합니다 유효한 콘텐츠 유형의 예는 아래에 표시되어 있으며, 각 프로그래밍 언어 라이브러리에 따라 약간의 차이가 있을 수 있습니다 content type application/json; charset=utf 8 zepeto 스튜디오에서 액세스 키 및 비밀 키 받기 jwt 인증 토큰을 생성하기 전에 zepeto 스튜디오 콘솔에서 액세스 키와 비밀 키를 받아야 합니다 📘 다음 가이드를 참조하십시오 오픈 api 관리하기 docid 1wn8 r5f0k 2bkrwzig5 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 카테고리의 플레이어 데이터 api를 기반으로 작성되었습니다 자바 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; 파이썬 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를 기반으로 작성되었습니다 자바 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; 파이썬 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 authorize 헤더를 사용하는 것과 같은 상대적으로 간단한 인증 방법을 구현하여 zepeto 서버에서 지원하는 기능 내에서 호출을 가능하게 합니다