Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from flask_cors import CORS
|
3 |
+
import jwt
|
4 |
+
import time
|
5 |
+
import uuid
|
6 |
+
import requests
|
7 |
+
import os
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
CORS(app, origins=[
|
11 |
+
"https://x-raremeta.com",
|
12 |
+
"https://cybercity.top",
|
13 |
+
"https://play-1.x-raremeta.com",
|
14 |
+
"https://play.cybercity.top",
|
15 |
+
"https://play.x-raremeta.com",
|
16 |
+
"https://www.x-raremeta.com",
|
17 |
+
"https://www.cybercity.top"])
|
18 |
+
|
19 |
+
# 你的配置信息
|
20 |
+
CLIENT_ID = "1243934778935"
|
21 |
+
PRIVATE_KEY_FILE_PATH = "private_key.pem"
|
22 |
+
KID = "tlrohMMZyKMrrpP3GtxF_3_cerDhVIMINs0LOW91m7w"
|
23 |
+
VALIDATION_TOKEN = "cybercity2025"
|
24 |
+
|
25 |
+
def generate_jwt(client_id, private_key, kid):
|
26 |
+
header = {
|
27 |
+
"alg": "RS256",
|
28 |
+
"typ": "JWT",
|
29 |
+
"kid": kid
|
30 |
+
}
|
31 |
+
payload = {
|
32 |
+
"iss": client_id,
|
33 |
+
"aud": "api.coze.cn",
|
34 |
+
"iat": int(time.time()),
|
35 |
+
"exp": int(time.time()) + 3600, # JWT 有效期为 1 小时
|
36 |
+
"jti": uuid.uuid4().hex # 防止重放攻击
|
37 |
+
}
|
38 |
+
return jwt.encode(payload, private_key, algorithm="RS256", headers=header)
|
39 |
+
|
40 |
+
def get_access_token(jwt_token):
|
41 |
+
url = "https://api.coze.cn/api/permission/oauth2/token"
|
42 |
+
data = {
|
43 |
+
"duration_seconds": 86399,
|
44 |
+
"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer"
|
45 |
+
}
|
46 |
+
headers = {
|
47 |
+
"Content-Type": "application/json",
|
48 |
+
"Authorization": f"Bearer {jwt_token}"
|
49 |
+
}
|
50 |
+
response = requests.post(url, json=data, headers=headers)
|
51 |
+
return response.json()
|
52 |
+
|
53 |
+
def get_token_from_flask():
|
54 |
+
auth_header = request.headers.get('Authorization')
|
55 |
+
if auth_header != VALIDATION_TOKEN:
|
56 |
+
return jsonify({"error": "Invalid authorization token"}), 401
|
57 |
+
try:
|
58 |
+
with open(PRIVATE_KEY_FILE_PATH, "r") as f:
|
59 |
+
private_key = f.read()
|
60 |
+
jwt_token = generate_jwt(CLIENT_ID, private_key, KID)
|
61 |
+
response = get_access_token(jwt_token)
|
62 |
+
if "access_token" in response:
|
63 |
+
return jsonify({
|
64 |
+
"access_token": response["access_token"],
|
65 |
+
"expires_in": response["expires_in"]
|
66 |
+
})
|
67 |
+
else:
|
68 |
+
return jsonify({"error": "Failed to get access token"}), 500
|
69 |
+
except Exception as e:
|
70 |
+
return jsonify({"error": str(e)}), 500
|
71 |
+
|
72 |
+
# --- Gradio 集成 ---
|
73 |
+
import gradio as gr
|
74 |
+
|
75 |
+
def get_token_for_gradio():
|
76 |
+
# 调用 Flask 应用逻辑获取 token
|
77 |
+
response, status_code = get_token_from_flask()
|
78 |
+
if status_code == 200:
|
79 |
+
return response.json
|
80 |
+
else:
|
81 |
+
return {"error": "Failed to get access token"}
|
82 |
+
|
83 |
+
iface = gr.Interface(
|
84 |
+
fn=get_token_for_gradio,
|
85 |
+
inputs=None,
|
86 |
+
outputs="json",
|
87 |
+
title="OAuth Access Token Generator"
|
88 |
+
)
|
89 |
+
|
90 |
+
if __name__ == '__main__':
|
91 |
+
iface.launch(server_name="0.0.0.0", server_port=5000)
|