| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import socket
- from fastapi import FastAPI, Request
- from fastapi.responses import HTMLResponse
- from fastapi.staticfiles import StaticFiles
- from fastapi.templating import Jinja2Templates
- import requests
- import uvicorn
- from fastapi.responses import FileResponse
- from fastapi import FastAPI, Depends, HTTPException, Form
- import httpx
- import os
- from db.user import UserOAuthToken
- from config import HOST, PORT
- app = FastAPI()
-
- @app.get("/")
- async def read_root(request: Request):
- return FileResponse(os.path.join("static", "index.html"))
- # 授权码获取 access_token 的路由
- @app.post("/get_access_token")
- async def get_access_token(request: Request):
- client_key = os.environ.get("CLIENT_KEY") # 从环境变量中获取 client_key
- client_secret = os.environ.get("CLIENT_SECRET") # 从环境变量中获取 client_secret
- try:
- code = request.query_params["code"] # 从查询参数中获取 code
- except KeyError:
- raise HTTPException(status_code=400, detail="Missing 'code' parameter")
-
- # 发送请求获取 access_token
- async with httpx.AsyncClient() as client:
- response = await client.post(
- "https://open.douyin.com/oauth/access_token/",
- headers={"Content-Type": "application/json"},
- json={
- "grant_type": "authorization_code",
- "client_key": client_key,
- "client_secret": client_secret,
- "code": code,
- },
- )
-
- if response.status_code != 200:
- raise HTTPException(status_code=response.status_code, detail=response.text)
-
- data = response.json()["data"]
-
- print(data)
-
- return {"message": "Access token stored successfully"}
- @app.get("/verify_callback")
- async def verify_callback(request: Request):
- # 打印请求方法
- print(f"Method: {request.method}")
-
- # 打印请求头
- print(f"Headers:")
- for key, value in request.headers.items():
- print(f"{key}: {value}")
- # 打印查询参数
- print(f"Query Parameters:")
- for key, value in request.query_params.items():
- print(f"{key}: {value}")
- return HTMLResponse("<h1>Callback Received! Verification Successful.</h1>")
- def main():
- uvicorn.run(app, host=HOST, port=PORT, log_level="info")
- if __name__ == "__main__":
- main()
|