60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
|
||
|
|
def build_wecom_oauth_url(corp_id: str, redirect_uri: str, state: str = "") -> str:
|
||
|
|
params = {
|
||
|
|
"appid": corp_id,
|
||
|
|
"redirect_uri": redirect_uri,
|
||
|
|
"response_type": "code",
|
||
|
|
"scope": "snsapi_base",
|
||
|
|
}
|
||
|
|
query = "&".join(f"{k}={v}" for k, v in params.items())
|
||
|
|
url = f"https://open.weixin.qq.com/connect/oauth2/authorize?{query}"
|
||
|
|
if state:
|
||
|
|
url += f"&state={state}"
|
||
|
|
url += "#wechat_redirect"
|
||
|
|
return url
|
||
|
|
|
||
|
|
|
||
|
|
def build_mp_oauth_url(app_id: str, redirect_uri: str, state: str = "", scope: str = "snsapi_userinfo") -> str:
|
||
|
|
params = {
|
||
|
|
"appid": app_id,
|
||
|
|
"redirect_uri": redirect_uri,
|
||
|
|
"response_type": "code",
|
||
|
|
"scope": scope,
|
||
|
|
}
|
||
|
|
query = "&".join(f"{k}={v}" for k, v in params.items())
|
||
|
|
url = f"https://open.weixin.qq.com/connect/oauth2/authorize?{query}"
|
||
|
|
if state:
|
||
|
|
url += f"&state={state}"
|
||
|
|
url += "#wechat_redirect"
|
||
|
|
return url
|
||
|
|
|
||
|
|
|
||
|
|
async def handle_wecom_oauth_callback(code: str, corp_id: str, corp_secret: str) -> dict[str, Any]:
|
||
|
|
import httpx
|
||
|
|
|
||
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo"
|
||
|
|
async with httpx.AsyncClient() as client:
|
||
|
|
resp = await client.get(url, params={"access_token": "", "code": code})
|
||
|
|
return resp.json()
|
||
|
|
|
||
|
|
|
||
|
|
async def handle_mp_oauth_callback(code: str, app_id: str, app_secret: str) -> dict[str, Any]:
|
||
|
|
import httpx
|
||
|
|
|
||
|
|
url = "https://api.weixin.qq.com/sns/oauth2/access_token"
|
||
|
|
async with httpx.AsyncClient() as client:
|
||
|
|
resp = await client.get(
|
||
|
|
url,
|
||
|
|
params={
|
||
|
|
"appid": app_id,
|
||
|
|
"secret": app_secret,
|
||
|
|
"code": code,
|
||
|
|
"grant_type": "authorization_code",
|
||
|
|
},
|
||
|
|
)
|
||
|
|
return resp.json()
|