Python 请求,没有属性“body”

芥末虎

我正在尝试运行类似于此问题中的代码:如何使用 HMAC-SHA512 和 Python 请求库签署 POST 请求?

我有以下代码:

import requests
import hmac
import hashlib
from itertools import count
import time

headers = { 'nonce': '',
        'Key' : 'myKey',
        'Sign': '',}
payload = { 'command': 'returnCompleteBalances',
            'account': 'all'}
secret = 'mySecret'

NONCE_COUNTER = count(int(time.time() * 1000))
headers['nonce'] = next(NONCE_COUNTER)

request = requests.Request(
    'POST', 'https://poloniex.com/tradingApi',
    params=payload, headers=headers)
signature = hmac.new(secret, request.body, digestmod=hashlib.sha512)
request.headers['Sign'] = signature.hexdigest()

with requests.Session() as session:
    response = session.send(request)

以下行:

signature = hmac.new(secret, request.body, digestmod=hashlib.sha512)

引发此错误:“请求”对象没有属性“正文”

汉车

你的源代码有几个问题:

  1. 对于 POST 方法,您不能使用参数params,但您需要参数data
  2. 如前所述,您需要该.prepare()方法。
  3. 该参数也nonce需要在 中指定payload,而不是在 中headers

这应该有效:

import requests
import hmac
import hashlib
from itertools import count
import time

NONCE_COUNTER = count(int(time.time() * 1000))

headers = { 'Key' : 'myKey',
            'Sign': '',}

payload = { 'nonce': next(NONCE_COUNTER),
            'command': 'returnCompleteBalances',
            'account': 'all'}

secret = 'mySecret'


request = requests.Request(
    'POST', 'https://poloniex.com/tradingApi',
    data=payload, headers=headers).prepare()
signature = hmac.new(secret, request.body, digestmod=hashlib.sha512)
request.headers['Sign'] = signature.hexdigest()


with requests.Session() as session:
    response = session.send(request)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

AttributeError:“ Response”对象没有适用于python的“ body_as_unicode”属性

来自分类Dev

Python:对象没有属性

来自分类Dev

Python属性错误对象没有属性

来自分类Dev

属性错误:没有属性“ completeKey”-Python

来自分类Dev

属性错误:没有属性“ completeKey”-Python

来自分类Dev

Python属性错误对象没有属性

来自分类Dev

没有定义scrapy python请求

来自分类Dev

Python请求没有响应

来自分类Dev

Python发布请求没有响应

来自分类Dev

Python请求挂起,而CURL没有挂起(相同请求)

来自分类Dev

X类没有属性Y(Python)

来自分类Dev

python float对象没有属性'value'

来自分类Dev

Python Tkinter实例没有属性“ tk”

来自分类Dev

Python CSV没有属性“作家”

来自分类Dev

Python的“ str”对象没有“读取”属性

来自分类Dev

Python错误-int对象没有属性

来自分类Dev

元组对象没有属性附加(python)

来自分类Dev

'tuple'对象没有属性'rstrip'python

来自分类Dev

Python-对象没有属性“ randint”

来自分类Dev

'NoneType'对象没有属性'sendall'PYTHON

来自分类Dev

Python'NoneType'对象没有属性'attrs'

来自分类Dev

python AttributeError'dict'对象没有属性

来自分类Dev

Python:“ NoneType”对象没有属性“ get”

来自分类Dev

Python:“对象没有属性”(pyTsetlinMachine)

来自分类Dev

python'str'对象没有属性'items'

来自分类Dev

Python对象没有属性错误

来自分类Dev

Python错误-int对象没有属性

来自分类Dev

Python错误模块没有属性令牌

来自分类Dev

Python AttributeError:“模块”没有属性“ currentanimation”