mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-09-12 00:29:27 -04:00
Fix fetch_access_token in openai
This commit is contained in:
parent
be9b8f796c
commit
5b2efa4aaf
@ -63,7 +63,7 @@ class OpenaiChat(AsyncGeneratorProvider):
|
||||
last_message = new_message
|
||||
|
||||
@classmethod
|
||||
def fetch_access_token(cls) -> str:
|
||||
def browse_access_token(cls) -> str:
|
||||
try:
|
||||
from selenium.webdriver.common.by import By
|
||||
from selenium.webdriver.support.ui import WebDriverWait
|
||||
@ -85,19 +85,25 @@ class OpenaiChat(AsyncGeneratorProvider):
|
||||
finally:
|
||||
driver.quit()
|
||||
|
||||
@classmethod
|
||||
async def fetch_access_token(cls, cookies: dict, proxies: dict = None) -> str:
|
||||
async with StreamSession(proxies=proxies, cookies=cookies, impersonate="chrome107") as session:
|
||||
async with session.get(f"{cls.url}/api/auth/session") as response:
|
||||
response.raise_for_status()
|
||||
auth = await response.json()
|
||||
if "accessToken" in auth:
|
||||
return auth["accessToken"]
|
||||
|
||||
@classmethod
|
||||
async def get_access_token(cls, cookies: dict = None, proxies: dict = None) -> str:
|
||||
if not cls._access_token:
|
||||
cookies = cookies if cookies else get_cookies("chat.openai.com")
|
||||
async with StreamSession(proxies=proxies, cookies=cookies, impersonate="chrome107") as session:
|
||||
async with session.get(f"{cls.url}/api/auth/session") as response:
|
||||
response.raise_for_status()
|
||||
auth = await response.json()
|
||||
if "accessToken" in auth:
|
||||
cls._access_token = auth["accessToken"]
|
||||
cls._access_token = cls.fetch_access_token()
|
||||
if not cls._access_token:
|
||||
raise RuntimeError("Missing access token")
|
||||
if cookies:
|
||||
cls._access_token = await cls.fetch_access_token(cookies, proxies)
|
||||
if not cls._access_token:
|
||||
cls._access_token = cls.browse_access_token()
|
||||
if not cls._access_token:
|
||||
raise RuntimeError("Read access token failed")
|
||||
return cls._access_token
|
||||
|
||||
@classmethod
|
||||
|
Loading…
Reference in New Issue
Block a user