why this code return api signature not valid in huobi api?

32 views Asked by At

i try to run this code

#!/bin/bash
source BBSW.sh
declare -g HTXApiKey=""
declare -g HTXSecretKey=""
declare -g HTXIdApi=""
function _Init_Huobi_ApiSecretIDData() {
    if [ -z "$1" ]; then
        echo "Errore: Nome exchange mancante."
        return 1
    fi
    exchange_name="$1"
    if [ "$2" == "DEBUG" ]; then
        echo "Debug mode attivato."
    fi
    db_file="ConfigDB.db"
    query="SELECT ApiKey, SecreatKey, IDapi FROM key_exchange WHERE exchange = '$exchange_name';"
    result=$(sqlite3 "$db_file" "$query")
    if [ -n "$result" ]; then
        HTXApiKey=$(echo "$result" | cut -d '|' -f 1)
        HTXSecretKey=$(echo "$result" | cut -d '|' -f 2)
        HTXIdApi=$(echo "$result" | cut -d '|' -f 3)
        if [ "$2" == "DEBUG" ]; then
            echo "Dati dell'exchange $exchange_name:"
            echo "HTXApiKey: $HTXApiKey"
            echo "HTXSecretKey: $HTXSecretKey"
            echo "HTXIdApi: $HTXIdApi"
        fi
    else
        echo "Exchange $exchange_name non trovato nel database."
    fi
}
response=""
_Huobi_Get_all_Accounts_of_the_Current_User() {
    _Init_Huobi_ApiSecretIDData "huobi"
    local PRE_URL="v1/account/accounts"
    local apiUrl="$HTXApiUrl$PRE_URL"
    local ACCESSKEYID="AccessKeyId=$HTXApiKey"
    local SIGNATUREVERSION="SignatureVersion=2"
    local SIGNATUREMETHOD="SignatureMethod=HmacSHA256"
    local TIMESTAMP="Timestamp=$(date -u +"%Y-%m-%dT%H:%M:%S" | sed 's/:/%3A/g')"
    local QUERYSTRING=$(echo -n "$ACCESSKEYID&$SIGNATUREMETHOD&$SIGNATUREVERSION&$TIMESTAMP" | sort)
    local SIGNATURE=$(echo -n "GET\n$HTXApiUrl\n$PRE_URL\n$QUERYSTRING" | openssl dgst -sha256 -hmac $HTXSecretKey | cut -c 18-)
    local SIGNATUREPARAM="Signature=$SIGNATURE"
    response=$(curl -s -H "Content-Type: application/json" -X GET "$apiUrl?$QUERYSTRING&$SIGNATUREPARAM")
    echo "curl -H 'Content-Type: application/json' -X GET \"$apiUrl?$QUERYSTRING&$SIGNATUREPARAM\""

}
_Initialize_Api_Huobi --api-typo "huobi"
_Huobi_Get_all_Accounts_of_the_Current_User
echo "$response"

but always return:

{"status":"error","err-code":"api-signature-not-valid","err-msg":"Signature not valid: Verification failure [校验失败]","data":null}

i tryed also this code

#!/bin/bash

# Set API keys
access_key=""
secret_key=""
host="api.huobi.pro"
path="/v1/account/accounts"
method="GET"

# Generates a timestamp in UTC format
timestamp() {
    date -u +"%Y-%m-%dT%H:%M:%S"
}

# URL-encode parameters
urlencode() {
    local string="${1}"
    local strlen=${#string}
    local encoded=""
    local pos c o

    for (( pos=0 ; pos<strlen ; pos++ )); do
       c=${string:$pos:1}
       case "$c" in
           [-_.~a-zA-Z0-9] ) o="${c}" ;;
           * )               printf -v o '%%%02x' "'$c"
       esac
       encoded+="${o}"
    done
    echo "${encoded}"
}

# Generate the signature
generate_signature() {
    local params="AccessKeyId=$access_key&SignatureVersion=2&SignatureMethod=HmacSHA256&Timestamp=$(urlencode "$(timestamp)")"
    local pre_signed_string="$method\n$host\n$path\n$params"
    echo -en "${pre_signed_string}" | openssl dgst -sha256 -hmac "${secret_key}" -binary | base64
}

# Send the GET request
send_request() {
    local signature=$(generate_signature)
    local signed_params="AccessKeyId=$access_key&SignatureVersion=2&SignatureMethod=HmacSHA256&Timestamp=$(urlencode "$(timestamp)")&Signature=$(urlencode "$signature")"
    local request_url="https://${host}${path}?${signed_params}"

    echo "Full request URL: $request_url"
    curl -s -H "Content-Type: application/x-www-form-urlencoded" -X GET "${request_url}"
}

# Main execution
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    response=$(send_request)
    echo "Response: $response"
fi

but nothing return always {"status":"error","err-code":"api-signature-not-valid","err-msg":"Signature not valid: Verification failure [校验失败]","data":null}, but i follow the manual https://www.htx.com/en-us/opend/newApiPages/?id=419 anyone have some idea? thanks

0

There are 0 answers