在Python中从etrade获取oauth请求令牌

时间:2015-02-18 08:57:21

标签: python oauth etrade-api

我尝试使用Python中的etrade api(sandbox)获取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 /标题。

知道我做错了吗?

2 个答案:

答案 0 :(得分:6)

etrade的一位乐于助人的人士澄清了所有oauth api请求(无论你是否在沙箱中工作)需要发送到主要的api网址:' https://etws.etrade.com/oauth/ { API}&#39 ;.

只有在对会话进行身份验证后才能使用沙盒网址:' https://etwssandbox.etrade.com/ {non-oauth-module} / sandbox / rest / {api}'。

如果其他人在使用python3中的etrade验证会话时遇到问题,这至少可以在沙箱中使用:

from rauth import OAuth1Service
import webbrowser 

def getSession():
    # Create a session
    # Use actual consumer secret and key in place of 'foo' and 'bar'
    service = OAuth1Service(
              name = 'etrade',
              consumer_key = 'foo',
              consumer_secret = 'bar',
              request_token_url = 'https://etws.etrade.com/oauth/request_token',
              access_token_url = 'https://etws.etrade.com/oauth/access_token',
              authorize_url = 'https://us.etrade.com/e/t/etws/authorize?key={}&token={}',
              base_url = 'https://etws.etrade.com')

    # Get request token and secret    
    oauth_token, oauth_token_secret = service.get_request_token(params = 
                                  {'oauth_callback': 'oob', 
                                   'format': 'json'})

    auth_url = service.authorize_url.format(consumer_key, oauth_token)

    # Get verifier (direct input in console, still working on callback)
    webbrowser.open(auth_url)
    verifier = input('Please input the verifier: ')

    return service.get_auth_session(oauth_token, oauth_token_secret, params = {'oauth_verifier': verifier})

# Create a session
session = getSession()

# After authenticating a session, use sandbox urls
url = 'https://etwssandbox.etrade.com/accounts/sandbox/rest/accountlist.json'

resp = session.get(url, params = {'format': 'json'})

print(resp)

答案 1 :(得分:1)

感谢@ethann-截至2020年6月17日,它实际上仍然可以使用更改的URL。

from rauth import OAuth1Service
import webbrowser 

service = OAuth1Service(
          name = 'etrade',
          consumer_key = 'foo',
          consumer_secret = 'bar',
          request_token_url = 'https://apisb.etrade.com/oauth/request_token',
          access_token_url = 'https://apisb.etrade.com/oauth/access_token',
          authorize_url = 'https://us.etrade.com/e/t/etws/authorize?key={}&token={}',
          base_url = 'https://apisb.etrade.com')

oauth_token, oauth_token_secret = service.get_request_token(params =
       {'oauth_callback': 'oob', 
        'format': 'json'})

auth_url = service.authorize_url.format('foo again', oauth_token)
webbrowser.open(auth_url)
verifier = input('Please input the verifier: ')
session = service.get_auth_session(oauth_token, oauth_token_secret, params = {'oauth_verifier': verifier})

url = 'https://apisb.etrade.com/v1/accounts/list'
resp = session.get(url, params = {'format': 'json'})

print(resp.text)