我正在尝试使用这个东西从 Python 中的 etrade api(沙箱)获取 oauth 请求令牌:
import requests
from oauthlib.oauth1 import Client
consumer_key = 'foo' # actual key used
consumer_secret = 'bar' # actual secret used
request_url = 'https://etwssandbox.etrade.com/oauth/sandbox/request_token'
client = Client(consumer_key, client_secret = consumer_secret)
uri, headers, body = client.sign(request_url)
add_params = ', realm="", oauth_token= "", oauth_callback="oob"'
headers['Authorization'] += add_params
r = requests.get(url = uri, headers = headers)
print(r.text) # abbreviated resp: " . . . .auth_problem=consumer_key_rejected,oauth_problem_advice=The oauth_consumer_key foo can be used only in SANDBOX environment . . .
生成的标头是:
{'Authorization': 'OAuth oauth_nonce="99985873301258063061424248905", oauth_timestamp="1424248905", oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="foo", oauth_signature="A7ZY91UyZz6NfSGmMA5YWGnVM%2FQ%3D", realm="", oauth_token= "", oauth_callback="oob"'}
我也试过网址:' https://etwssandbox.etrade.com/oauth/sandbox/rest/request_token '
而且我已经尝试了没有 add_params 的标题(它似乎需要空白的 oauth_token?)
注意:令人困惑的是,响应会定期返回:“Http/1.1 400 Bad Request”具有完全相同的 url/header。
知道我做错了什么吗?