@@ -2772,58 +2772,6 @@ def _find_account(self, mailbox: str) -> dict[str, Any]:
27722772 raise MailServiceError (f"Graph 接口账号池中不存在该邮箱: { target } " )
27732773 raise MailServiceError (f"Graph 账号文件中不存在该邮箱: { target } " )
27742774
2775- @staticmethod
2776- def _extract_api_account_payload (payload : Any ) -> dict [str , Any ]:
2777- if isinstance (payload , dict ):
2778- for key in ("data" , "item" , "account" , "result" , "row" ):
2779- val = payload .get (key )
2780- if isinstance (val , dict ):
2781- return val
2782- return payload
2783- if isinstance (payload , list ):
2784- for it in payload :
2785- if isinstance (it , dict ):
2786- return it
2787- return {}
2788-
2789- @staticmethod
2790- def _pick_str_value (* pairs : tuple [dict [str , Any ], tuple [str , ...]]) -> str :
2791- for src , keys in pairs :
2792- if not isinstance (src , dict ):
2793- continue
2794- for key in keys :
2795- val = src .get (key )
2796- if val is None :
2797- continue
2798- text = str (val ).strip ()
2799- if text :
2800- return text
2801- return ""
2802-
2803- def _load_api_account_detail (self , account_id : str , * , proxies : Any = None ) -> dict [str , Any ]:
2804- aid = str (account_id or "" ).strip ()
2805- if not aid :
2806- return {}
2807- status , payload , text = self ._api_request (
2808- "GET" ,
2809- f"/api/accounts/{ urllib .parse .quote (aid , safe = '' )} " ,
2810- proxies = proxies ,
2811- timeout = 25 ,
2812- )
2813- if status == 404 :
2814- return {}
2815- if not (200 <= status < 300 ):
2816- if self ._is_api_token_invalid (status , payload , text ):
2817- raise MailServiceError (
2818- "Graph 接口模式鉴权失败:开放接口 token 无效。"
2819- "请检查 graph_api_token 是否与上游 MAIL_API_TOKEN 一致。"
2820- )
2821- detail = self ._api_error_message (payload , text )
2822- raise MailServiceError (
2823- f"Graph 接口读取账号详情失败: /api/accounts/{ aid } HTTP { status } : { detail or text } "
2824- )
2825- return self ._extract_api_account_payload (payload )
2826-
28272775 def mark_account_registered (
28282776 self ,
28292777 mailbox : str ,
@@ -2843,69 +2791,28 @@ def mark_account_registered(
28432791 if not account_id :
28442792 return False
28452793
2846- detail = {}
2847- try :
2848- detail = self ._load_api_account_detail (account_id , proxies = proxies )
2849- except Exception :
2850- detail = {}
2851-
2852- account_val = self ._pick_str_value (
2853- (detail , ("account" , "email" , "address" , "mailbox" )),
2854- (acc , ("email" ,)),
2855- )
2856- password_val = self ._pick_str_value (
2857- (detail , ("password" ,)),
2858- (acc , ("password" ,)),
2859- )
2860- client_id_val = self ._pick_str_value (
2861- (detail , ("clientId" , "client_id" )),
2862- (acc , ("client_id" , "clientId" )),
2863- )
2864- refresh_token_val = self ._pick_str_value (
2865- (detail , ("refreshToken" , "refresh_token" )),
2866- (acc , ("refresh_token" , "refreshToken" )),
2867- )
2868- if not account_val or "@" not in account_val :
2869- account_val = target
2870-
2871- missing : list [str ] = []
2872- if not account_val :
2873- missing .append ("account" )
2874- if not password_val :
2875- missing .append ("password" )
2876- if not client_id_val :
2877- missing .append ("clientId" )
2878- if not refresh_token_val :
2879- missing .append ("refreshToken" )
2880- if missing :
2881- raise MailServiceError (
2882- "Graph 接口更新账号备注失败:缺少字段 "
2883- + ", " .join (missing )
2884- )
2885-
28862794 body = {
2887- "account" : account_val ,
2888- "password" : password_val ,
2889- "clientId" : client_id_val ,
2890- "refreshToken" : refresh_token_val ,
28912795 "remark" : str (remark or "已注册" ).strip () or "已注册" ,
28922796 }
28932797 status , payload , text = self ._api_request (
2894- "PUT " ,
2895- f"/api/accounts/{ urllib .parse .quote (account_id , safe = '' )} " ,
2798+ "PATCH " ,
2799+ f"/api/open/ accounts/{ urllib .parse .quote (account_id , safe = '' )} /remark " ,
28962800 json_body = body ,
28972801 proxies = proxies ,
28982802 timeout = 25 ,
28992803 )
29002804 if not (200 <= status < 300 ):
2805+ if self ._is_api_token_invalid (status , payload , text ):
2806+ raise MailServiceError (
2807+ "Graph 接口模式鉴权失败:开放接口 token 无效。"
2808+ "请检查 graph_api_token 是否与上游 MAIL_API_TOKEN 一致。"
2809+ )
29012810 detail_msg = self ._api_error_message (payload , text )
29022811 raise MailServiceError (
2903- f"Graph 接口更新账号备注失败: /api/accounts/{ account_id } HTTP { status } : { detail_msg or text } "
2812+ "Graph 接口更新账号备注失败: "
2813+ f"/api/open/accounts/{ account_id } /remark HTTP { status } : { detail_msg or text } "
29042814 )
29052815
2906- acc ["password" ] = password_val
2907- acc ["client_id" ] = client_id_val
2908- acc ["refresh_token" ] = refresh_token_val
29092816 acc ["remark" ] = body ["remark" ]
29102817 return True
29112818
0 commit comments