gateio / gateapi-python

250 stars 92 forks source link

Gate api exception, label: INVALID_SIGNATURE, message: Signature mismatch #32

Closed zh0uquan closed 4 years ago

zh0uquan commented 4 years ago

请问哪里有问题会导致签名不匹配, key和secret分别改成1和2了。

import gate_api
from gate_api.exceptions import ApiException, GateApiException

HOST = "https://api.gateio.ws/api/v4"
API_KEY = "1"
API_SECRET = "2"

# Defining the host is optional and defaults to https://api.gateio.ws/api/v4
# See configuration.py for a list of all supported configuration parameters.
configuration = gate_api.Configuration(
    host=HOST,
    key=API_KEY,
    secret=API_SECRET,
)

api_client = gate_api.ApiClient(configuration)
# Create an instance of the API class
api_withdrawal = gate_api.WithdrawalApi(api_client)

try:
    # List all futures contracts
    api_response = api_withdrawal.withdraw(
        ledger_record='{"currency":"ttt","address":"tttt","amount":"10.1","memo":""}'
    )
    print(api_response)
except GateApiException as ex:
    print("Gate api exception, label: %s, message: %s\n" % (
    ex.label, ex.message))
except ApiException as e:
    print(
        "Exception when calling DeliveryApi->list_delivery_contracts: %s\n" % e)
zh0uquan commented 4 years ago

测试了WalletApi的list_withdrawals,没有Signature mismatch

zh0uquan commented 4 years ago

https://github.com/gateio/gateapi-python/blob/master/gate_api/api_client.py#L179-L186 这里加密函数中的URL参数是host和resource_path的字符组合, 而在文档中加密函数中的参数url中是api版本和resource_path的字符组合:

url = '/withdrawals'
query_param = ''
body='{"currency":"ETH","address":"1HkxtBAMrA3tP5ENnYY2CZortjZvFDH5Cs","amount":"222.61","memo":""}'
# `gen_sign` 的实现参考认证一章
sign_headers = gen_sign('POST', prefix + url, query_param, body)
headers.update(sign_headers)
r = requests.request('POST', host + prefix + url, headers=headers, data=body)
print(r.json())

请修复。

revilwang commented 4 years ago

API 封装的实现没有问题,如果你查看 update_param_for_auth 的实现会看到,生成签名前,url 会单纯只取出 path 部分 urlparse(url).path

你这边问题应该是 withdraw 的参数传递方式错了,你可以看一下提现的文档示例 ,传入 withdraw() 函数的是有一个 gate_api.LedgerRecord 对象,而不是字符串

revilwang commented 4 years ago

其他类似的需要传入 body 或者返回 body 的请求,都是传入一个 python object,返回的格式也都是已经做好 json 解析并封装成 python object 返回的

zh0uquan commented 4 years ago

OK 谢谢