Python rauth - POST 请求有效吗?
Python rauth - do POST requests work?
我有一个使用 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 请求添加身份验证内容?
好吧,我没有深入了解失败的 rauth POST 请求,但我找到了替代解决方案。
我正在使用来自 etradePythonApi 的 etrade 访问函数 (https://github.com/haualan/etradePythonAPI/blob/master/etradepy.py), with the exception that I stripped out the splinter & pyvirtualdisplay code that was attempting to scrape/insert login details into the etrade verification page, and instead replaced it with the oob code from ethann's answer on this page:
直接使用请求而不是通过 rauth 包装器,一切都运行良好,这似乎证实了 POST 请求没有在 rauth 中获得 Oauth 祝福。
让我的程序可供其他人使用和改编
我有一个使用 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 请求添加身份验证内容?
好吧,我没有深入了解失败的 rauth POST 请求,但我找到了替代解决方案。
我正在使用来自 etradePythonApi 的 etrade 访问函数 (https://github.com/haualan/etradePythonAPI/blob/master/etradepy.py), with the exception that I stripped out the splinter & pyvirtualdisplay code that was attempting to scrape/insert login details into the etrade verification page, and instead replaced it with the oob code from ethann's answer on this page:
直接使用请求而不是通过 rauth 包装器,一切都运行良好,这似乎证实了 POST 请求没有在 rauth 中获得 Oauth 祝福。
让我的程序可供其他人使用和改编