tedchou12 / webull

Unofficial APIs for Webull.
MIT License
601 stars 184 forks source link

Automatic Way To Refresh Access Token? #300

Closed Alanavai closed 2 years ago

Alanavai commented 2 years ago

Hello,

I've been using the workaround to refresh the access token by logging into Webull and copying the credentials in the inspector, but is there any currently working way this can be done automatically?

Thanks

ssheikh098 commented 2 years ago

Hello,

Any specific reason you are using above manual way instead of calling login method of this code? I am using this package from some time now(almost year) and all the time I used login module. Let me know if any way I can help you.

In my local copy of this package I did some modification as well. Some of the things I did, is checking token expire time and then calling login module

This code written on old version of this package so there might be few functionality already added to this package however below code should work if you are using email MFA. also I am using outlook.com so if its gmail then related imap should be updated.

Note - below code might have lot of dead code. Email read code copied from google and modified to read only email received from Webull based on subject and sender.

call set_access_token method to load access token from pickle file saves after login, if there is no entry created yet, then call start session. Below code can be improved a lot but as its working for me I didn't spend time correcting code. Sample Program: image

Supporting program(Webull_Ext class) image

image

image

image

image

image

I have added an additional parameter to login method as I have 2 account with Webull so to pick zone from first account I am passing 1

image

Thanks

Alanavai commented 2 years ago

Hi,

I appreciate the effort you put into your reply, thanks. However, I'm still a bit confused by all this custom stuff you added. Could you show me how to login and automatically refresh the access token using the built-in functionality of the Webull class instead of showing your own class?

Edit: Actually, I think I got it working again by looking through some old code. I'll post my snippet here in case it helps someone else.

def authenticate(self):
  with open("webull_credentials.json", "r") as credentials:
    credentials_data = json.load(credentials)

  # This part gets currently valid tokens/login data
  self.wb._refresh_token = credentials_data['refreshToken']
  self.wb._access_token = credentials_data['accessToken']
  self.wb._token_expire = credentials_data['tokenExpireTime']
  self.wb._uuid = credentials_data['uuid']
  self.wb.get_account_id()
  self.wb.get_trade_token(config.pin)

  # This part replaces currently valid tokens with new expiration times
  refresh = self.wb.refresh_login()
  credentials_data["refreshToken"] = refresh["refreshToken"]
  credentials_data["accessToken"] = refresh["accessToken"]
  credentials_data["tokenExpireTime"] = refresh["tokenExpireTime"]
  with open("webull_credentials.json", 'w') as file:
    json.dump(credentials_data, file)

Note: From what I've seen, you must have a valid access token first in order to use the refresh method. The refresh portion is called after the API establishes a connection, not before (which was my mistake).