python - 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)
不太确定为什么会出现此错误,我认为这与从全局签名、令牌和其他方面调用有关。如您所知,在编码方面我有点新手
解决方案
推荐阅读
- javascript - 变量和函数的名称
- wso2 - 如何更新 wso2is 组件
- android - 我编译我的 kivy 应用程序在使用 buildozer 编译到 android 应用程序时显示空白
- python-3.x - 鼠标光标指向方法或函数时的python 3.8上下文帮助
- python - 列表理解和“所有”关键字
- android - 如何避免在单个事件的视图模型上使用 LiveData?
- c# - 如何在 C# 中异步执行 MSCRM 工作流
- apache-spark - 用于存储类似日志的 Parquet 行组大小
- mysql - 做关系分解的原因
- python - 你能告诉我为什么我只收到 * 作为输出而不是最初输入的字符串,作为解密的字符串?