NelsonDane / public-invest-api

Unofficial reverse-engineered Public.com Invest API written in Python Requests
MIT License
13 stars 6 forks source link

Multi login #10

Closed Thesyjaga closed 3 months ago

Thesyjaga commented 3 months ago

So I have 2 py files and 1 is where I have my account so I made my wife an account and when I go to the api and setup a py for her since its in the same laptop it is saying that

{"code":null,"message":"Invalid credentials."} Traceback (most recent call last): File "C:\Users\thesy\Documents\Python\Public\public2.py", line 4, in public.login( File "C:\Users\thesy\AppData\Local\Programs\Python\Python312\Lib\site-packages\public_invest_api\public.py", line 80, in login raise Exception("Login failed, check credentials") Exception: Login failed, check credentials

This is my code right now ( i took off the login info)

import sys import yfinance as yf from public_invest_api import Public

Function to get the current price of a stock using yfinance

def get_current_price(stock_symbol): stock_data = yf.Ticker(stock_symbol) current_price = stock_data.history(period="1d")["Close"].iloc[-1] return current_price

public = Public() public.login( username='@gmail.com', password='', wait_for_2fa=True # When logging in for the first time, you need to wait for the SMS code )

Ask whether to buy or sell

while True: action = input("Enter '1' to BUY or '2' to SELL: ") if action == '1': action = 'BUY' break elif action == '2': action = 'SELL' break else: print("Invalid input. Please enter '1' to BUY or '2' to SELL.")

Assuming you have user input for symbol and quantity

stock_symbol = input("Enter the stock ticker: ") quantity = int(input("Enter the quantity: "))

Get the current price of the stock

current_price = get_current_price(stock_symbol)

Adjust price by rounding up and adding 1 cent

current_price += 0.001 current_price = round(current_price, 2)

Place order

order = public.place_order( symbol=stock_symbol, quantity=quantity, side=action, order_type='LIMIT',
limit_price=current_price, time_in_force='DAY', # 'GTC' or 'IOC' or 'FOK' is_dry_run=False, # If True, it will not actually place the order tip=0 # The amount to tip Public.com )

print(order)

NelsonDane commented 3 months ago

Hello, I believe I know what the issue is. When you logged into your account, it saved a pkl file with your login cookies using the default filename. When you then tried to log in to your wife's account, it then tried to use your saved cookies, which resulted in the error because it was for your account and not hers. To fix this, specify a different filename when creating the Public() object, like public = Public(filename="public1.pkl") for your account, then public = Public(filename="public2.pkl" for her account for example. That way it will use the correct saved cookies for each of your accounts.

Thesyjaga commented 3 months ago

so do I need to remove the "public = Public()" and then add "public = Public(filename="public1.pkl")" and "public = Public(filename="public2.pkl")"

Thesyjaga commented 3 months ago

and I had a simlar issue with robinhood also do you think that this method would even fix that?

NelsonDane commented 3 months ago

I'm not sure, but there's no hurt in trying!

Thesyjaga commented 3 months ago

what about this?

so do I need to remove the "public = Public()" and then add "public = Public(filename="public1.pkl")" and "public = Public(filename="public2.pkl")"

NelsonDane commented 3 months ago

yes.