Can';t在iconomi使用python中的sha512对消息进行签名

Can';t在iconomi使用python中的sha512对消息进行签名,python,api,authentication,sha512,Python,Api,Authentication,Sha512,我正在尝试通过API在发送经过身份验证的消息。 我习惯于在处理其他exchange API时对消息进行签名,但无法使用此特定API进行身份验证 我阅读了用于身份验证的文档: 通过使用 预灰化字符串时间戳+方法上的base64解码密钥+ requestPath+body(其中+表示字符串连接)和 base64对输出进行编码,其中: 时间戳值与ICN-timestamp头相同。身体 是请求正文字符串,如果没有请求正文,则省略 (通常用于GET请求)。方法必须始终为大写 示例:base64_编码(HM

我正在尝试通过API在发送经过身份验证的消息。 我习惯于在处理其他exchange API时对消息进行签名,但无法使用此特定API进行身份验证

我阅读了用于身份验证的文档:

通过使用 预灰化字符串时间戳+方法上的base64解码密钥+ requestPath+body(其中+表示字符串连接)和 base64对输出进行编码,其中:

时间戳值与ICN-timestamp头相同。身体 是请求正文字符串,如果没有请求正文,则省略 (通常用于GET请求)。方法必须始终为大写

示例:base64_编码(HMAC_SHA512(密钥,时间戳+大写字母(方法)+请求路径+正文))

我还在上找到了一个java客户端示例,请参见下面的java签名生成:

private String generateServerDigest(String method, String uri, long timestamp, String body) {
    //return timestamp + request.getMethodValue() + uri + body;
    String checkDigestString = timestamp + method + uri + body;//  "GET+/v1/daa-list+123123123"; //timestamp in epoch milliseconds

    // hash server composited digest with algorithm and apikeys secret
    SecretKeySpec signingKey = new SecretKeySpec(apiSecret.getBytes(), "HmacSHA512");
    Mac mac;
    try {
        mac = Mac.getInstance(signingKey.getAlgorithm());
        mac.init(signingKey);
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        log.warn("Could not ={}", signingKey.getAlgorithm());
        return null;
    }

    return Base64.getEncoder().encodeToString(mac.doFinal(checkDigestString.getBytes()));
}
请注意,checkDigestString代码
timestamp+method+uri+body
和comment
GET+/v1/daa列表+123123
在官方文档上已经不同了


这是我的python实现尝试:

def sign(timestamp,method,requestPath,body):
    global api_secret
    base64_decoded_secret_key = base64.b64decode(api_secret)
    content_to_hash = (str(timestamp) + method.upper() + requestPath + body).encode('utf-8')
    sign_digest = hmac.new(base64_decoded_secret_key, content_to_hash , hashlib.sha512).digest()    
    return base64.b64encode(sign_digest).decode('utf-8')
当我使用
requestPath=“/v1/user/balance”
(需要进行身份验证)尝试此签名方法时,它会失败而不会出错



java和python中使用的任何一种方法都可以帮助我将此签名方法转换为python吗?

此代码适用于GET:

import time,requests
import hashlib,hmac,base64

api_key = "my api key"
api_secret = "my api secret"

defaut_encoding = "utf8"

uri = "https://api.iconomi.com"
requestPath = "/v1/user/balance"
api_url_target = uri+requestPath # https://api.iconomi.com/v1/user/balance
method="GET"
body=""
icn_timestamp = int(1000.*time.time())

message = (str(icn_timestamp) + method.upper() + requestPath + body).encode(defaut_encoding)
signature_digest = hmac.new(api_secret.encode(defaut_encoding), message, hashlib.sha512).digest() #here digest is byte
b64_signature_digest= base64.b64encode(signature_digest).decode(defaut_encoding)

headers_sign= {
    "ICN-API-KEY":api_key,
    "ICN-SIGN":b64_signature_digest,
    "ICN-TIMESTAMP":str(icn_timestamp)
}

s=requests.session()
res = s.get(api_url_target,headers=headers_sign,timeout=3, verify=True).content
print (res)
更新@Karl comment,此代码适用于POST:

import time,requests
import hashlib,hmac,base64,json

api_key = "my api key"
api_secret = "my api secret"
ticker = "my ticker strategy"


defaut_encoding = "utf8"

uri = "https://api.iconomi.com"
requestPath = "/v1/strategies/"+ticker+"/structure"
api_url_target = uri+requestPath # https://api.iconomi.com/v1/strategies/{my ticker strategy}/structure
method="POST"
body="{'ticker': ticker, 'values': [{'rebalancedWeight': 1., 'targetWeight':1., 'assetTicker': 'XMR', 'assetName': 'Monero', 'assetCategory': 'Privacy'}]}"
icn_timestamp = int(1000.*time.time())

message = (str(icn_timestamp) + method.upper() + requestPath + body).encode(defaut_encoding)
signature_digest = hmac.new(api_secret.encode(defaut_encoding), message, hashlib.sha512).digest() #here digest is byte
b64_signature_digest= base64.b64encode(signature_digest).decode(defaut_encoding)

headers_sign= {
    "ICN-API-KEY":api_key,
    "ICN-SIGN":b64_signature_digest,
    "ICN-TIMESTAMP":str(icn_timestamp)
}

s=requests.session()
res = s.post(api_url_target,headers=headers_sign,json = json.loads(body), timeout=3, verify=True).content
print (res)

这对post/更新是否也适用?我试图更新我的结构,但得到了这个错误b{“timestamp”:1617618493409,“path”:“/v1/strategies/PRINCIPLES/structure”,“status”:401,“error”:“Unauthorized”,“message”:“,”requestId:“031154a9-10370867”}不,POST请求不应该这样工作,请允许我有时间仔细检查一下如何处理它!太感谢你了!♥♥♥你能胜任一个职位吗?GET为我工作,但由于某些原因,POST返回401未经授权