mirror of
https://github.com/lanqian528/chat2api.git
synced 2026-06-13 21:02:46 +08:00
69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
import json
|
|
import os
|
|
import random
|
|
import time
|
|
from functools import cache
|
|
|
|
from fastapi import HTTPException
|
|
|
|
from utils.Client import Client
|
|
from utils.Logger import logger
|
|
from utils.config import proxy_url_list
|
|
|
|
|
|
DATA_FOLDER = "data"
|
|
REFRESH_MAP_FILE = os.path.join(DATA_FOLDER, "refresh_map.json")
|
|
|
|
if not os.path.exists(DATA_FOLDER):
|
|
os.makedirs(DATA_FOLDER)
|
|
|
|
|
|
@cache
|
|
def load_refresh_map():
|
|
if os.path.exists(REFRESH_MAP_FILE):
|
|
with open(REFRESH_MAP_FILE, "r") as file:
|
|
return json.load(file)
|
|
else:
|
|
return {}
|
|
|
|
|
|
def save_refresh_map(refresh_map):
|
|
with open(REFRESH_MAP_FILE, "w") as file:
|
|
json.dump(refresh_map, file)
|
|
|
|
|
|
async def rt2ac(refresh_token):
|
|
refresh_map = load_refresh_map()
|
|
if refresh_token in refresh_map and int(time.time()) - refresh_map.get(refresh_token, {}).get("timestamp", 0) < 24 * 60 * 60:
|
|
access_token = refresh_map[refresh_token]["token"]
|
|
logger.info(f"refresh_token -> access_token from cache")
|
|
return access_token
|
|
else:
|
|
access_token = await chat_refresh(refresh_token)
|
|
refresh_map[refresh_token] = {"token": access_token, "timestamp": int(time.time())}
|
|
save_refresh_map(refresh_map)
|
|
logger.info(f"refresh_token -> access_token with openai: {access_token}")
|
|
return access_token
|
|
|
|
|
|
async def chat_refresh(refresh_token):
|
|
data = {
|
|
"client_id": "pdlLIX2Y72MIl2rhLhTE9VV9bN905kBh",
|
|
"grant_type": "refresh_token",
|
|
"redirect_uri": "com.openai.chat://auth0.openai.com/ios/com.openai.chat/callback",
|
|
"refresh_token": refresh_token
|
|
}
|
|
client = Client(proxy=random.choice(proxy_url_list) if proxy_url_list else None)
|
|
try:
|
|
r = await client.post("https://auth0.openai.com/oauth/token", json=data, timeout=5)
|
|
if r.status_code == 200:
|
|
access_token = r.json()['access_token']
|
|
return access_token
|
|
else:
|
|
raise Exception("Unknown or invalid refresh token.")
|
|
except Exception as e:
|
|
raise HTTPException(status_code=401, detail=str(e))
|
|
finally:
|
|
await client.close()
|
|
del client
|