0

I'm trying to get this Etrade stuff up an running.... so far i have:

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 = 'cabf024eaXXXXXXXXX7a0243d8d',  
              consumer_secret = '3d05c41XXXXXXXXX1949d07c',
              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('cabf0XXXXXXXXXa0243d8d', 
oauth_token)

    webbrowser.open(auth_url)
    verifier = raw_input('Please input the verifier: ')

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

session = getSession()

This authentication process works perfectly fine and allows me to do get/delete requests but when I attempt to make post requests:

url = 
'https://etwssandbox.etrade.com/order/sandbox/rest/previewoptionorder'
para = {
    "PreviewOptionOrder": {
     "-xmlns": "http://order.etws.etrade.com",
     "OptionOrderRequest": {
       "accountId": "83550325",
       "quantity": "4",
       "symbolInfo": {
         "symbol": "AAPL",
         "callOrPut": "CALL",
         "strikePrice": "585",
         "expirationYear": "2012",
         "expirationMonth": "07",
         "expirationDay": "21"
       }, 
       "orderAction": "BUY_OPEN",

                           "priceType": "MARKET",
       "orderTerm": "GOOD_FOR_DAY"
     }
   }  
 }
resp = session.post(url,data=para)
resp.text

I get an error:

Unauthorized request: consumer key is missing.

I've tried numerous things (granted I am new to this stuff). I tried authenticating using just requests to no avail and I tried passing the oauth1 object to the posts function as a kw argument. Any ideas?

Babosura
  • 21
  • 3
  • That usually happens when you don't use the correct expected format for your data. Maybe you should import json and use json.dumps(para). Maybe you need to put correct quotes. Maybe it is expecting a string. All this really dependes on how the post method process the data keyword. – Alex Sep 02 '17 at 23:38
  • Hmm.. well I am using an example directly from the Etrade site which is what made me skeptical... ill have a second look – Babosura Sep 03 '17 at 04:01
  • Of course I can be wrong. But, I've been there with requests and urllib2 libraries (never used this webrowser) for a long time. You copy paste some API `official` stuff, everything works except when you pass lots of data. Two things break the usual libraries implementation: nested dictionary(you have that) and special characters. I would try to send a minimal post request with a simple dictionary and see if it works. Thing is: check if the problem is POST or the data you are posting. . – Alex Sep 03 '17 at 17:53
  • You hit the nail on the head! had to use json.dumps() and add headers... thank you thank you thank you! – Babosura Sep 03 '17 at 18:54
  • Hey! No problem! Enjoy! – Alex Sep 03 '17 at 19:59

0 Answers0