首页 > 解决方案 > b'{"error": {"message": "HMAC authentication key and signature is given, but they are invalid.", "error_code": 41}}'

问题描述

我遇到了 API 调用/配置问题。相同的 API 密钥在其他地方工作,所以我知道它不是密钥,并且密钥确实具有正确的权限。

import datetime
import hashlib
import hmac as hmac_lib
import json
import requests
import ssl
import time
import re
import os
import time
import io
import imaplib
import urllib.parse
from lbcapi import api
# Creds

hmac_key = 'My_key'
hmac_secret = 'My_secret'
username = 'My_username'

# pull dash (straight into data list)
connection = api.hmac('My_key',
                      'My_secret')


def getDashboard():
    global nonce
    global signature
    nonce = str(int(datetime.datetime.timestamp(datetime.datetime.now())*1000000))
    arguments = {}
    get_or_post_params_urlencoded = urllib.parse.urlencode(arguments)
    url = 'https://localbitcoins.com/api/dashboard/'
    relative_path = '/api/dashboard/'
    message = nonce + hmac_key + relative_path + get_or_post_params_urlencoded
    signature = hmac_lib.new(bytes(hmac_secret, 'utf-8'), msg=bytes(message,
                             'utf-8'), digestmod=hashlib.sha256).hexdigest().upper()
    headers = {'Apiauth-Key': hmac_key, 'Apiauth-Nonce': nonce, 'Apiauth-Signature': signature}
    req = requests.get(url, headers=headers, params=get_or_post_params_urlencoded)
    dashboard = json.loads(req.text)
    return dashboard['data']
# pull message


def gettrademessages(contact_id):
    nonce = str(int(datetime.datetime.timestamp(datetime.datetime.now())*1000000))
    arguments = {}
    get_or_post_params_urlencoded = urllib.parse.urlencode(arguments)
    url = 'https://localbitcoins.com/api/contact_messages/%d/' % contact_id
    relative_path = '/api/contact_messages/%d/' % contact_id
    message = nonce + hmac_key + relative_path + get_or_post_params_urlencoded
    signature = hmac_lib.new(bytes(hmac_secret, 'utf-8'), msg=bytes(message,
                             'utf-8'), digestmod=hashlib.sha256).hexdigest().upper()
    headers = {'Apiauth-Key': hmac_key, 'Apiauth-Nonce': nonce, 'Apiauth-Signature': signature}
    req = requests.get(url, headers=headers, params=get_or_post_params_urlencoded)
    dict11 = json.loads(req.text)
    return dict11

# /api/contact_message_post/{contact_id}/


while True:
    localBitcoinsDashboard = localBitcoinsDashboardContacts = []
    trade_type = contact_id = buyer = payment_completed_at = contact_reference = seller = amount = payment_completed_at = messages = None
    print(datetime.datetime.now())
    localBitcoinsDashboardContacts = getDashboard()['contact_list']
    if len(localBitcoinsDashboardContacts) > 0:
        print("You have open trades")
        for contact in localBitcoinsDashboardContacts:
            trade_type = contact['data']['advertisement']['trade_type']
            contact_id = contact['data']['contact_id']
            buyer = contact['data']['buyer']
            payment_completed_at = contact['data']['payment_completed_at']
            contact_reference = contact['data']['reference_code']
            seller = contact['data']['seller']
            amount = contact['data']['amount']
            # mesages = gettrademessages(contact_id)
        # Autoresponder
        mesages = gettrademessages(contact_id)['data']['message_count']
        if mesages <= 2:
            print('sending message ')
            print('yeet')
            head = {
                "msg": "this is a message / alert",
                'Apiauth-Key': hmac_key,
                'Apiauth-Nonce': nonce,
                'Apiauth-Signature': signature,
            }

            cid = contact['data']['contact_id']
            resp = requests.post(f"https://localbitcoins.com/api/contact_message_post/{cid}", headers=head)
            print(resp.content)

        else:
            print('waiting for trade.......')

    time.sleep(10)

不太确定为什么会出现此错误,我认为这与从全局签名、令牌和其他方面调用有关。如您所知,在编码方面我有点新手

标签: pythonapi

解决方案


推荐阅读