我有一个使用 python2.7 和 rauth 与 etrade api 通信的应用程序。
获取 etrade 验证令牌并设置授权会话有效,我可以成功执行 GET 访问,如下所示:
url = 'https://etwssandbox.etrade.com/accounts/sandbox/rest/accountlist.json'
response = session.get(url, params = {'format': 'json'}, header_auth=True)
我在 POST 访问方面根本没有任何成功,尽管进行了很多搜索,但还没有找到一个示例来显示 post 与 rauth 的使用。我在尝试:
url = 'https://etwssandbox.etrade.com/order/sandbox/rest/placeequityorder'
id = datetime.datetime.now().strftime('%y%m%d%H%M')+ticker
payload = {
"PlaceEquityOrder": {
"-xmlns": "http://order.etws.etrade.com",
"EquityOrderRequest": {
"accountId": account,
"clientOrderId": id,
"limitPrice": "",
"quantity": qty,
"symbol": ticker,
"orderAction": "BUY",
"priceType": "MARKET",
"marketSession": "REGULAR",
"orderTerm": "GOOD_FOR_DAY"
}
}
}
response = session.post(url, payload, header_auth=True)
我从 etrade 得到的回应是:
{
'cookies':<RequestsCookieJar [
] >,
'_content':'<Error>\n <message>oauth_problem=signature_invalid</message>\n</Error>',
'headers':{
'Content-Length':'69',
'Expires':'Sat, 21 May 1995 12:00:00 GMT',
'Keep-Alive':'timeout=60, max=400',
'apiServerName':'20w44m3',
'Connection':'Keep-Alive',
'Pragma':'no-cache',
'Cache-Control':'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
'Date':'Wed, 17 May 2017 15:16:42 GMT',
'Server':'Apache',
'WWW-Authenticate':'OAuth realm=https://etws.etrade.com/,oauth_problem=signature_invalid'
},
'url': u'https://etwssandbox.etrade.com/order/sandbox/rest/placeequityorder',
'status_code':401,
'_content_consumed':True,
'encoding':None,
'request':<PreparedRequest [
POST
] >,
'connection':<requests.adapters.HTTPAdapter object at 0x7f3187f71790>,
'elapsed':datetime.timedelta(0,
0,
149246 ),
'raw':<requests.packages.urllib3.response.HTTPResponse object at 0x7f3186c7ab90>,
'reason':'Unauthorized',
'history':[
]
}
我假设我对 POST 请求做错了什么,但是 rauth 库是否有可能没有将身份验证内容添加到 POST 请求中?