인증 및 권한
토큰 및 비밀키(Secret Key) 신청 또는 수정은 개인 센터 → Open API에서 진행해 주세요.
API 권한 검증
클라이언트가 거래 API를 요청할 때, 모든 요청에 다음 헤더가 반드시 포함되어야 합니다:
Nonce, Token, Signature.
예시:
Python
$header[] = 'Nonce: 1534927978_ab43c';
$header[] = 'Token: 57ba172a6be125cca2f449826f9980ca';
$header[] = 'Signature: v490hupi0s0bckcp6ivb69p921';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
Token
사용자 액세스 토큰입니다. 개인 센터 → Open API에서 확인할 수 있습니다.
Nonce
요청마다 생성되는 임의의 값입니다.
형식: 타임스탬프_임의문자열
예시: 1534927978_ab43c
규칙: - 시간 오차는 60초를 초과할 수 없습니다. - 각 Nonce는 한 번만 사용할 수 있습니다.
Signature
token, secret_key, nonce, $_POST, $_GET을 사용하여 생성된 요청 서명입니다.
서버는 서명을 검증하여 요청이 권한이 있는 사용자에 의해 생성되었는지 확인합니다.
서명 생성을 위한 Python 예시
Python
def sign(Nonce, data=dict()):
tmp = list()
tmp.append(TOKEN)
tmp.append(SECRET)
tmp.append(Nonce)
for d, x in data.items():
tmp.append(str(d) + "=" + str(x))
return hashlib.sha1(''.join(sorted(tmp))).hexdigest()
서명 생성을 위한 PHP 예시
PHP
private function signature($nonce)
{
$token = TOKEN;
$secret_key = SECRET_KEY;
$tmpArr = array($token, $secret_key, $nonce);
foreach($_GET as $k => $v){
$tmpArr[] = $k . "=" . $v;
}
foreach($_POST as $k => $v){
$tmpArr[] = $k . "=" . $v;
}
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$signature = sha1($tmpStr);
return $signature;
}
서명 생성을 위한 Java 예시
Java
import org.apache.commons.codec.digest.DigestUtils;
import java.security.SecureRandom;
import java.util.*;
public class SignatureUtil {
private static final char[] CHAR_POOL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
private static final SecureRandom RANDOM = new SecureRandom();
/**
* SHA1 서명 생성
*
* @param params 요청 매개변수 (Map)
* @param token 토큰
* @param secretKey 비밀키
* @return 서명 결과 (SHA1 Hex)
*/
public static HashMap<String, String> getSignature(Map<String, Object> params, String token, String secretKey) {
List<String> paramList = new ArrayList<>();
String nonce = generateNonce(5, 10);
paramList.add(token);
paramList.add(secretKey);
paramList.add(nonce);
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
paramList.add(entry.getKey() + "=" + entry.getValue());
}
}
paramList.sort(String::compareToIgnoreCase);
String joined = String.join("", paramList);
HashMap<String, String> result = new HashMap<>();
result.put("nonce", nonce);
result.put("sign", DigestUtils.sha1Hex(joined));
return result;
}
/**
* Nonce 생성: 타임스탬프_임의문자열
*/
public static String generateNonce(int randomLength, int timestampLength) {
long currentMillis = System.currentTimeMillis();
String timestamp = String.valueOf(currentMillis);
if (timestampLength < timestamp.length()) {
timestamp = timestamp.substring(0, timestampLength);
} else {
while (timestamp.length() < timestampLength) {
timestamp += "0";
}
}
char[] buf = new char[randomLength];
for (int i = 0; i < randomLength; i++) {
buf[i] = CHAR_POOL[RANDOM.nextInt(CHAR_POOL.length)];
}
return timestamp + "_" + new String(buf);
}
}
예시
Text Only
가정:
token = "57ba172a6be125c"
secret_key = "ca2f449826f9980ca"
nonce = "1534927978_ab43c"
요청 URL:
/openApi/entrust/currentList
요청 매개변수:
symbol = "BTC-USDT"
type = "1"
정렬된 문자열:
1534927978_ab43c57ba172a6be125cca2f449826f9980casymbol=BTC-USDTtype=1
SHA1 결과:
signature = 731faa3d170bb746a767cea58ae563830594e1fe