拿到token
This commit is contained in:
50
core/flow.py
50
core/flow.py
@@ -28,6 +28,9 @@ class RegisterFlow:
|
||||
AUTH_VALIDATE_OTP = "https://auth.openai.com/api/accounts/email-otp/validate"
|
||||
AUTH_COMPLETE_PROFILE = "https://auth.openai.com/api/accounts/create_account"
|
||||
|
||||
# 获取 Token 相关
|
||||
CHATGPT_SESSION = "https://chatgpt.com/api/auth/session"
|
||||
|
||||
def __init__(self, session: OAISession, config, email: Optional[str] = None, password: Optional[str] = None):
|
||||
self.s = session
|
||||
self.config = config
|
||||
@@ -41,6 +44,7 @@ class RegisterFlow:
|
||||
self.csrf_token: Optional[str] = None
|
||||
self.sentinel_token: Optional[Dict[str, Any]] = None
|
||||
self.otp: Optional[str] = None
|
||||
self.access_token: Optional[str] = None
|
||||
|
||||
logger.info(f"RegisterFlow initialized for {self.email} (oai-did: {self.s.oai_did})")
|
||||
|
||||
@@ -68,12 +72,14 @@ class RegisterFlow:
|
||||
await self._step6_send_email_otp()
|
||||
await self._step7_submit_otp()
|
||||
await self._step8_complete_profile()
|
||||
await self._step9_get_access_token()
|
||||
|
||||
logger.success(f"[{self.email}] Registration completed successfully! ✅")
|
||||
return {
|
||||
"email": self.email,
|
||||
"password": self.password,
|
||||
"oai_did": self.s.oai_did,
|
||||
"access_token": self.access_token,
|
||||
"status": "success",
|
||||
"message": "Account registered successfully"
|
||||
}
|
||||
@@ -239,14 +245,54 @@ class RegisterFlow:
|
||||
resp = self.s.post(
|
||||
self.AUTH_COMPLETE_PROFILE,
|
||||
json={"name": name, "birthdate": birthdate},
|
||||
headers={"Content-Type": "application/json", "Referer": self.AUTH_CREATE_ACCOUNT}
|
||||
headers={"Content-Type": "application/json", "Referer": self.AUTH_CREATE_ACCOUNT},
|
||||
allow_redirects=False
|
||||
)
|
||||
|
||||
if resp.status_code != 200:
|
||||
if resp.status_code not in [200, 302, 303]:
|
||||
raise RuntimeError(f"Profile completion failed: {resp.status_code}")
|
||||
|
||||
# 检查是否有 continue_url 需要跟随
|
||||
try:
|
||||
data = resp.json()
|
||||
continue_url = data.get("continue_url")
|
||||
if continue_url:
|
||||
logger.info(f"[{self.email}] Following OAuth callback...")
|
||||
if not continue_url.startswith("http"):
|
||||
continue_url = f"https://auth.openai.com{continue_url}"
|
||||
|
||||
# 跟随 OAuth 回调,最终会重定向到 chatgpt.com
|
||||
callback_headers = {
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Referer": "https://auth.openai.com/",
|
||||
"Sec-Fetch-Dest": "document",
|
||||
"Sec-Fetch-Mode": "navigate",
|
||||
"Sec-Fetch-Site": "same-origin",
|
||||
"Upgrade-Insecure-Requests": "1",
|
||||
}
|
||||
self.s.get(continue_url, headers=callback_headers, allow_redirects=True)
|
||||
logger.info(f"[{self.email}] ✓ OAuth callback completed")
|
||||
except Exception as e:
|
||||
logger.debug(f"[{self.email}] No continue_url or parse error: {e}")
|
||||
|
||||
logger.info(f"[{self.email}] ✓ Profile completed")
|
||||
|
||||
async def _step9_get_access_token(self):
|
||||
"""Step 9: 通过登录流程获取 Access Token"""
|
||||
logger.info(f"[{self.email}] Step 9: Getting access token via login")
|
||||
|
||||
from core.login_flow import LoginFlow
|
||||
|
||||
# 使用当前 session 执行登录流程
|
||||
login_flow = LoginFlow(self.s, self.email, self.password)
|
||||
result = await login_flow.run()
|
||||
|
||||
if result.get("status") == "success":
|
||||
self.access_token = result.get("access_token")
|
||||
logger.info(f"[{self.email}] ✓ Access token obtained: {self.access_token[:50]}...")
|
||||
else:
|
||||
logger.warning(f"[{self.email}] Failed to get access token: {result.get('error')}")
|
||||
|
||||
def _generate_email(self) -> str:
|
||||
"""生成随机邮箱"""
|
||||
random_part = secrets.token_hex(8)
|
||||
|
||||
Reference in New Issue
Block a user