Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions python/device-oauth/main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
import json
from datetime import datetime

from cozepy import DeviceOAuthApp, load_oauth_app_from_config
from cozepy import DeviceOAuthApp, load_oauth_app_from_config, Coze, TokenAuth, User

COZE_OAUTH_CONFIG_PATH = "coze_oauth_config.json"


def load_coze_oauth_app(config_path) -> DeviceOAuthApp:
def load_app_config(config_path) -> dict:
with open(config_path, "r") as file:
config = file.read()
return json.loads(config)


return load_oauth_app_from_config(config) # type: ignore
def load_coze_oauth_app(config_path) -> DeviceOAuthApp:
try:
with open(config_path, "r") as file:
config = json.loads(file.read())
return load_oauth_app_from_config(config) # type: ignore
except FileNotFoundError:
raise Exception(
f"Configuration file not found: {config_path}. Please make sure you have created the OAuth configuration file."
)
except Exception as e:
raise Exception(f"Failed to load OAuth configuration: {str(e)}")


def timestamp_to_datetime(timestamp: int) -> str:
return datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")


app_config = load_app_config(COZE_OAUTH_CONFIG_PATH)
coze_oauth_app = load_coze_oauth_app(COZE_OAUTH_CONFIG_PATH)


def users_me(access_token) -> User:
coze = Coze(auth=TokenAuth(access_token), base_url=app_config["coze_api_base"])

return coze.users.me()


if __name__ == "__main__":
coze_oauth_app = load_coze_oauth_app(COZE_OAUTH_CONFIG_PATH)

Expand All @@ -32,3 +55,8 @@ def timestamp_to_datetime(timestamp: int) -> str:
print(f"[device-oauth] refresh_token: {oauth_token.refresh_token}")
expires_str = timestamp_to_datetime(oauth_token.expires_in)
print(f"[device-oauth] expires_in: {oauth_token.expires_in} ({expires_str})")

user = users_me(oauth_token.access_token)
print(f"[user_info] user_id: {user.user_id}")
print(f"[user_info] user_name: {user.user_name}")
print(f"[user_info] nick_name: {user.nick_name}")
2 changes: 1 addition & 1 deletion python/device-oauth/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cozepy==0.10.1
cozepy==0.11.0
2 changes: 1 addition & 1 deletion python/jwt-oauth/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def load_app_config(config_path) -> dict:
def load_coze_oauth_app(config_path) -> JWTOAuthApp:
try:
with open(config_path, "r") as file:
config = file.read()
config = json.loads(file.read())
return load_oauth_app_from_config(config) # type: ignore
except FileNotFoundError:
raise Exception(
Expand Down
2 changes: 1 addition & 1 deletion python/jwt-oauth/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Flask==2.2.5
cozepy==0.10.1
cozepy==0.11.0
26 changes: 24 additions & 2 deletions python/pkce-oauth/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import secrets
from datetime import datetime

from cozepy import load_oauth_app_from_config, PKCEOAuthApp
from cozepy import load_oauth_app_from_config, PKCEOAuthApp, TokenAuth, Coze
from flask import Flask, redirect, request, session

app = Flask(
Expand All @@ -25,7 +25,7 @@ def load_app_config(config_path) -> dict:
def load_coze_oauth_app(config_path) -> PKCEOAuthApp:
try:
with open(config_path, "r") as file:
config = file.read()
config = json.loads(file.read())
return load_oauth_app_from_config(config) # type: ignore
except FileNotFoundError:
raise Exception(
Expand All @@ -52,6 +52,7 @@ def render_template(template: str, kwargs: dict) -> str:
if not kwargs:
kwargs = {}
kwargs["coze_www_base"] = app_config["coze_www_base"]
kwargs["coze_api_base"] = app_config["coze_api_base"]
template = read_html_template(template)
for key, value in kwargs.items():
template = template.replace(f"{{{{{key}}}}}", str(value))
Expand Down Expand Up @@ -184,5 +185,26 @@ def refresh_token():
return {"error": f"Failed to refresh token: {str(e)}"}, 500


@app.route("/users_me")
def users_me():
access_token = request.args.get("access_token")
if not access_token:
return render_template(
"websites/error.html", {"error": "Access token is required"}
)
coze = Coze(auth=TokenAuth(access_token), base_url=app_config["coze_api_base"])

try:
user = coze.users.me()
return {
"user_id": user.user_id,
"user_name": user.user_name,
"nick_name": user.nick_name,
"avatar_url": user.avatar_url,
}
except Exception as e:
return {"error": f"Failed to get user info: {str(e)}"}, 500


if __name__ == "__main__":
app.run(debug=False, use_reloader=False, port=8080)
2 changes: 1 addition & 1 deletion python/pkce-oauth/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Flask==2.2.5
cozepy==0.10.1
cozepy==0.11.0
26 changes: 24 additions & 2 deletions python/web-oauth/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import secrets
from datetime import datetime

from cozepy import load_oauth_app_from_config, WebOAuthApp
from cozepy import load_oauth_app_from_config, WebOAuthApp, Coze, TokenAuth
from flask import Flask, redirect, request, session

app = Flask(
Expand All @@ -25,7 +25,7 @@ def load_app_config(config_path) -> dict:
def load_coze_oauth_app(config_path) -> WebOAuthApp:
try:
with open(config_path, "r") as file:
config = file.read()
config = json.loads(file.read())
return load_oauth_app_from_config(config) # type: ignore
except FileNotFoundError:
raise Exception(
Expand All @@ -48,6 +48,7 @@ def render_template(template: str, kwargs: dict) -> str:
if not kwargs:
kwargs = {}
kwargs["coze_www_base"] = app_config["coze_www_base"]
kwargs["coze_api_base"] = app_config["coze_api_base"]
template = read_html_template(template)
for key, value in kwargs.items():
template = template.replace(f"{{{{{key}}}}}", str(value))
Expand Down Expand Up @@ -182,5 +183,26 @@ def refresh_token():
return {"error": f"Failed to refresh token: {str(e)}"}, 500


@app.route("/users_me")
def users_me():
access_token = request.args.get("access_token")
if not access_token:
return render_template(
"websites/error.html", {"error": "Access token is required"}
)
coze = Coze(auth=TokenAuth(access_token), base_url=app_config["coze_api_base"])

try:
user = coze.users.me()
return {
"user_id": user.user_id,
"user_name": user.user_name,
"nick_name": user.nick_name,
"avatar_url": user.avatar_url,
}
except Exception as e:
return {"error": f"Failed to get user info: {str(e)}"}, 500


if __name__ == "__main__":
app.run(debug=False, use_reloader=False, port=8080)
2 changes: 1 addition & 1 deletion python/web-oauth/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Flask==2.2.5
cozepy==0.10.1
cozepy==0.11.0
109 changes: 105 additions & 4 deletions shared/websites/callback.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,49 @@
.card-body {
padding: 3rem 2rem;
}
.logo-section {
display: flex;
align-items: center;
justify-content: center;
gap: 1rem;
margin: 0 auto 1.5rem;
}
.logo {
width: 64px;
height: 64px;
margin: 0 auto 1.5rem;
}
.user-avatar {
width: 64px;
height: 64px;
border-radius: 50%;
cursor: pointer;
position: relative;
}
.user-info-tooltip {
position: absolute;
background: white;
padding: 0.75rem 1rem;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
min-width: 200px;
top: 100%;
left: 50%;
transform: translateX(-50%);
margin-top: 0.5rem;
display: none;
z-index: 1000;
}
.user-info-tooltip.show {
display: block;
}
.user-info-item {
margin: 0.25rem 0;
font-size: 0.875rem;
color: #333;
}
.user-info-label {
color: #666;
margin-right: 0.5rem;
}
.token-section {
background-color: #f8f9ff;
Expand Down Expand Up @@ -150,9 +189,11 @@
<div class="card">
<div class="card-body">
<div class="text-center">
<a href="{{coze_www_base}}" target="_blank">
<img src="/assets/coze.png" alt="Coze Logo" class="logo">
</a>
<div class="logo-section" id="logoSection">
<a href="{{coze_www_base}}" target="_blank">
<img src="/assets/coze.png" alt="Coze Logo" class="logo">
</a>
</div>
<div class="status-bar"></div>
<h2 class="title">Authorization Successful</h2>
<p class="info-text">You have successfully authorized the application. Here's your authorization information:</p>
Expand Down Expand Up @@ -201,6 +242,7 @@ <h3>Access Token</h3>
// 隐藏 refresh token 行
document.querySelector('.refresh-token-row').style.display = 'none';
}
fetchUserInfo();
});

function showMessage(message, isSuccess = true) {
Expand Down Expand Up @@ -271,6 +313,65 @@ <h3>Access Token</h3>
console.error('Failed to copy text: ', err);
});
}

// 获取用户信息并展示
async function fetchUserInfo() {
const accessToken = document.getElementById('access_token').textContent;
const refreshTokenEle = document.getElementById('refresh_token');
const isJWT = !refreshTokenEle || !refreshTokenEle.textContent.trim();

if (isJWT) {
return; // JWT 模式不请求用户信息
}

try {
const response = await fetch(`/users_me?access_token=${accessToken}`);

if (!response.ok) {
throw new Error('Failed to fetch user info');
}

const userData = await response.json();

if (!userData.user_id) {
return; // 如果没有用户ID,不展示用户信息
}

// 创建用户头像和信息元素
const userAvatarContainer = document.createElement('div');
userAvatarContainer.style.position = 'relative';

const userAvatar = document.createElement('img');
userAvatar.src = userData.avatar_url;
userAvatar.alt = 'User Avatar';
userAvatar.className = 'user-avatar';

const userInfoTooltip = document.createElement('div');
userInfoTooltip.className = 'user-info-tooltip';
userInfoTooltip.innerHTML = `
<div class="user-info-item"><span class="user-info-label">User ID:</span>${userData.user_id}</div>
<div class="user-info-item"><span class="user-info-label">Username:</span>${userData.user_name}</div>
<div class="user-info-item"><span class="user-info-label">Nickname:</span>${userData.nick_name}</div>
`;

userAvatarContainer.appendChild(userAvatar);
userAvatarContainer.appendChild(userInfoTooltip);

// 添加鼠标事件
userAvatar.addEventListener('mouseenter', () => {
userInfoTooltip.classList.add('show');
});
userAvatar.addEventListener('mouseleave', () => {
userInfoTooltip.classList.remove('show');
});

// 将用户头像添加到logo区域
const logoSection = document.getElementById('logoSection');
logoSection.appendChild(userAvatarContainer);
} catch (error) {
console.error('Error fetching user info:', error);
}
}
</script>
</body>
</html>
Loading