def get_tokens(): consumer_key = os.environ.get("ETRADE_ACCESS_KEY") consumer_secret = os.environ.get("ETRADE_SECRET") oauth = pyetrade.ETradeOAuth(consumer_key, consumer_secret) print(oauth.get_request_token()) # Use the printed URL verifier_code = input("Enter verification code: ") tokens = oauth.get_access_token(verifier_code) print(tokens)
def __init__(self, key, secret, tokens=None, setup=False): self.consumer_key = key self.consumer_secret = secret if setup: oauth = pyetrade.ETradeOAuth(self.consumer_key, self.consumer_secret) print('Copy and paste this URL and follow the directions:') print(oauth.get_request_token()) verifier_code = input("Enter verification code: ") self.tokens = oauth.get_access_token(verifier_code) else: self.tokens = tokens
def __init__(self): #token_data = self.get_token() #if token_data: # self.tokens = dict() # self.oauth = pyetrade.ETradeOAuth(OAUTH_CONSUMER_KEY, CONSUMER_SECRET) # self.tokens['oauth_token'] = token_data[0] # self.tokens['oauth_token_secret'] = token_data[1] # self.token_creation_time = token_data[2] # print self.token_creation_time #else: # self.oauth = pyetrade.ETradeOAuth(OAUTH_CONSUMER_KEY, CONSUMER_SECRET) # token_verification_url = self.oauth.get_request_token() # click.echo(token_verification_url) # self.verifier_code = click.prompt("Please enter a verfier code", type=str) # self.tokens = self.get_access_token() self.tokens = dict() self.oauth = pyetrade.ETradeOAuth(OAUTH_CONSUMER_KEY, CONSUMER_SECRET) token_data = self.get_token() if not token_data: token_verification_url = self.oauth.get_request_token() click.echo(token_verification_url) self.verifier_code = click.prompt("Please enter a verfier code", type=str) self.tokens = self.get_access_token() try: self.add_token(self.tokens['oauth_token'], self.tokens['oauth_token_secret']) except Exception: raise Exception("Failed to add tokens") else: token_creation = token_data[2] diff_creation = now - token_creation token_access_time = token_data[3] diff_access_min = (now - token_access_time).total_seconds() / 60.0 if now.strftime("%Y-%m-%d") != token_creation.strftime("%Y-%m-%d"): token_verification_url = self.oauth.get_request_token() click.echo(token_verification_url) self.verifier_code = click.prompt("Please enter a verfier code", type=str) self.tokens = self.get_access_token() try: self.update_token(self.tokens['oauth_token'], self.tokens['oauth_token_secret']) except Exception: print("Failed to add tokens") elif now.strftime("%Y-%m-%d") == token_creation.strftime("%Y-%m-%d") and int(diff_access_min) > 120: self.tokens['oauth_token'] = token_data[0] self.tokens['oauth_token_secret'] = token_data[1] self.renew_token self.update_token(self.tokens['oauth_token'], self.tokens['oauth_token_secret'], last_access_time=now) else: self.tokens['oauth_token'] = token_data[0] self.tokens['oauth_token_secret'] = token_data[1]
def etrade_objects(): consumer_key = os.environ.get('ETRADE_CONSUMER_KEY') consumer_secret = os.environ.get('ETRADE_CONSUMER_SECRET') oauth = pyetrade.ETradeOAuth(consumer_key, consumer_secret) url = oauth.get_request_token() print('\n') verifier_code = input('Go Here: ' + url + '\n\nThen enter verification code: ') print('\n') tokens = oauth.get_access_token(verifier_code) access_manager = pyetrade.ETradeAccessManager(consumer_key, consumer_secret, tokens['oauth_token'], tokens['oauth_token_secret']) accounts = pyetrade.ETradeAccounts(consumer_key, consumer_secret, tokens['oauth_token'], tokens['oauth_token_secret']) market = pyetrade.ETradeMarket(consumer_key, consumer_secret, tokens['oauth_token'], tokens['oauth_token_secret']) order = pyetrade.ETradeOrder(consumer_key, consumer_secret, tokens['oauth_token'], tokens['oauth_token_secret']) etrade_objects = { 'consumer_key': consumer_key, 'consumer_secret': consumer_secret, 'oauth': oauth, 'verifier_code': verifier_code, 'tokens': tokens, 'url': url, 'access_manager': access_manager, 'accounts': accounts, 'market': market, 'order': order } return etrade_objects
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Overide profileTradeData data with command line data if clOptions.sandBox: sandBox = 1 # Set the proper consumer keys, sandbox or live if sandBox: consumerKey = sandConsumerKey consumerSecret = sandConsumerSecret print("Sandbox account is true: " + str(sandBox)) else: print("Live account: Sandbox value: " + str(sandBox)) oauth = pyetrade.ETradeOAuth(consumerKey, consumerSecret) # Use the printed URL URL = oauth.get_request_token() #os.system('open -a safari ' + URL) print("\n" + URL + "\n") verifierCode = input("Enter verification code: ") tokens = oauth.get_access_token(verifierCode) print("Generated token/secret:") print(tokens['oauth_token']) print(tokens['oauth_token_secret'])
import flask, pyetrade from flask import Flask, request, jsonify from azure.keyvault.secrets import SecretClient from azure.identity import DefaultAzureCredential from selenium import webdriver KVUri = f"https://brrkeys1.vault.azure.net" credential = DefaultAzureCredential() client = SecretClient(vault_url=KVUri, credential=credential) consumer_key = client.get_secret('etradeSandboxKey').value consumer_secret = client.get_secret('etradeSandboxSecret').value etrade_username = client.get_secret('etradeUsername').value etrade_password = client.get_secret('etradePassword').value oauth = pyetrade.ETradeOAuth(consumer_key, consumer_secret) tokenurl = oauth.get_request_token() driver = webdriver.Chrome() driver.get(tokenurl) driver.find_element_by_xpath('//*[@id="user_orig"]').send_keys('saitcho666') driver.find_element_by_xpath('//*[@id="log-on-form"]/div[2]/div[2]/div/input').send_keys('Xwingx12!!') driver.find_element_by_xpath('//*[@id="logon_button"]').click() driver.get(tokenurl) driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/form/input[3]').click() code = driver.find_element_by_xpath('/html/body/div[2]/div/div/input').getAttribute('value') tokens = oauth.get_access_token(code) app = Flask(__name__) app.config["DEBUG"] = True
def init_etrade_auth(self): # prod_api_key == client_key # prod_secret == client_secret # app_api_key == resource_owner_key # app_api_secret == resource_owner_secret self.etrade_auth_keys = {} # Instantiate client key and secret with open("e-trade-prod-api-key.txt") as client_key_file: self.etrade_auth_keys["client_key"] = client_key_file.read( ).rstrip() with open("e-trade-prod-api-secret.txt") as client_secret_file: self.etrade_auth_keys["client_secret"] = client_secret_file.read( ).rstrip() # Check age of resource owner key and secret app_key_secret_mtime = datetime.datetime.fromtimestamp( os.stat("e-trade-app-api-key.txt").st_mtime) app_key_and_secret_age = datetime.datetime.now() - app_key_secret_mtime # Get new keys if we have old app/resource_owner keys if app_key_and_secret_age.total_seconds() >= 119 * 60: print( "E-Trade App API Key and Secret are older than 2 hours, requesting new tokens..." ) # Get new request token from ETrade for new resource_owner key and secret self.oauth = pyetrade.ETradeOAuth( self.etrade_auth_keys["client_key"], self.etrade_auth_keys["client_secret"], ) # Shows URL to get verfication code print( "Go to the provided URL, approve access, and enter the token..." ) self.authorization_url = self.oauth.get_request_token() webbrowser.open(self.authorization_url, new=2) verifier_code = input("Enter verification code: ") tokens = self.oauth.get_access_token(verifier_code) self.etrade_auth_keys["resource_owner_key"] = tokens["oauth_token"] self.etrade_auth_keys["resource_owner_secret"] = tokens[ "oauth_token_secret"] with open("e-trade-app-api-key.txt", "w") as resource_owner_key_file: resource_owner_key_file.write( self.etrade_auth_keys["resource_owner_key"]) with open("e-trade-app-api-secret.txt", "w") as resource_owner_secret_file: resource_owner_secret_file.write( self.etrade_auth_keys["resource_owner_secret"]) print("Got ETrade Resource Owner Key and Secret Tokens!") else: with open("e-trade-app-api-key.txt") as resource_owner_key_file: self.etrade_auth_keys[ "resource_owner_key"] = resource_owner_key_file.read( ).rstrip() with open("e-trade-app-api-secret.txt" ) as resource_owner_secret_file: self.etrade_auth_keys[ "resource_owner_secret"] = resource_owner_secret_file.read( ).rstrip() print("Instantiating ETrade Access Manager...") self.access_man = pyetrade.authorization.ETradeAccessManager( self.etrade_auth_keys["client_key"], self.etrade_auth_keys["client_secret"], self.etrade_auth_keys["resource_owner_key"], self.etrade_auth_keys["resource_owner_secret"], ) print("Renewing App/Resource Owner Key and Secret...") self.access_man.renew_access_token() print("Showing E-Trade Keys...") self.pp.pprint(self.etrade_auth_keys) print("========================")