0

I followed the updated code found via cswiger found here Getting an oauth request token from etrade in Python

and updated for use via the production URLs while using my production key/secret. I'm posting fine and able to use the session to return data specific to my account while testing in both the sandbox and prod with respective urls/key/secret. When I call end points for Options like ''https://apisb.etrade.com/v1/market/optionchains?symbol=AAPL' I get the error "oauth_problem=signature_invalid" in both test/prod. I've used the sample code from eTrade and the prior mentioned link which both use rauth-OAuth1Session.

from rauth import OAuth1Service
import webbrowser 
parser = ConfigParser(interpolation = None)

secret = parser.get('etrade','CONSUMER_SECRET')
key = parser.get('etrade','CONSUMER_KEY')

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

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

auth_url = service.authorize_url.format(key, 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://api.etrade.com/v1/accounts/list'
#url = 'https://api.etrade.com/v1/market/optionchains?symbol=AAPL'
resp = session.get(url, params = {'format': 'json'})

print(resp.text)

I've tried building the signature directly following OAuth/eTrade instructions while passing the updated headers along but I'm still getting the same error. Thanks for your feedback on this.

oauth_consumer_secret = parser.get('etrade','CONSUMER_SECRET')
oauth_consumer_key = parser.get('etrade','CONSUMER_KEY')
oauth_token_secret = oauth_token_secret
oauth_token = oauth_token
oauth_verification_code = verifier
oauth_signature_method="HMAC-SHA1"
#oauth_signature = session.signature
oauth_timestamp = int(time.time())
rand_str = lambda n: ''.join([random.choice(string.hexdigits) for i in range(n)])
oauth_nonce = rand_str(40)

uri= 'https://api.etrade.com/oauth/request_token&format=json'

oauth_callback='oob'
base1 = '&oauth_callback='

targetKey = oauth_consumer_secret
base2 = '&oauth_consumer_key='

nonce = str(oauth_nonce)
base3 = '&oauth_nonce='

oauth_signature_method='HMAC-SHA1'
base4 = '&oauth_signature_method='

timeStamp = str(oauth_timestamp)
base5 = '&oauth_timestamp='

oauth_version='1.0'
base6 = '&oauth_version='

prebase = uri+base1+oauth_callback+base2+targetKey+base3+nonce+base4+oauth_signature_method+base5+timeStamp+base6+oauth_version 
url1 = urllib.parse.quote_plus(prebase)
addGet = 'GET&'+url1

header_string = {'Authorization: OAuth oauth_consumer_key="{}",'.format(oauth_consumer_key)+\
                 'oauth_timestamp="{:d}",'.format(oauth_timestamp)+\
                 'oauth_nonce="{}",'.format(urllib.parse.quote_plus(oauth_nonce))+\
                 'oauth_signature_method="{}",'.format(oauth_signature_method)+\
                 'oauth_signature="{}",'.format(addGet)+\
                 'oauth_token="{}"'.format(oauth_token)}

url = 'https://api.etrade.com/v1/market/optionchains?symbol=AAPL'
resp = session.get(url, params = {'format': 'json'},  headers = header_string)
print(resp.text)
  • What happens here? webbrowser.open(auth_url) This should require you to login, then accept some terms and conditions, then you'll see a verifier you have to copy and enter manually into the script. If you are doing that then your script should be ok and your session is created. You can see your session tokens like this. print(session.access_token, session.access_token_secret) – Jonathan Leon Nov 16 '20 at 01:04
  • Exactly. The workflow is fine and I'm able to get the required tokens and access my account info and other protected resources. It just does not work for "options" end points. I actually found another package on git called pyetrade that uses requests_oauthlib import OAuth1Session and NOT rauth import OAuth1Service as is provided via the eTrade sample code/workflow and the requests_oauthlib import OAuth1Session worked to enable whatever is needed to get that signature issues corrected. I've not done a deep dive/root cause but this approach enabled me to return options data from eTrade. – user14572003 Nov 17 '20 at 15:30

0 Answers0