优化OAuth客户端的日志输出,增强调试信息;修复获取访问令牌时的端口配置,确保回调地址正确;更新API配置中的服务器地址和端口。

This commit is contained in:
2025-08-06 16:53:46 +08:00
parent 1b5f7ac50a
commit 21c01d51d5
4 changed files with 77 additions and 29 deletions
+42 -6
View File
@@ -62,7 +62,7 @@ export class OAuthClient {
redirect_uri: this.config.redirectUri,
state: state
});
return `${this.config.serverUrl}/oauth/authorize?${params.toString()}`;
}
@@ -72,7 +72,12 @@ export class OAuthClient {
* @returns 访问令牌响应
*/
async getAccessToken(code: string): Promise<TokenResponse | null> {
console.log('this.config.serverUrl', this.config.serverUrl);
console.log('🔧 OAuth配置信息:', {
serverUrl: this.config.serverUrl,
clientId: this.config.clientId,
redirectUri: this.config.redirectUri
});
const url = `${this.config.serverUrl}/oauth/token`;
const data = new URLSearchParams({
grant_type: 'authorization_code',
@@ -82,6 +87,14 @@ export class OAuthClient {
redirect_uri: this.config.redirectUri
});
console.log('🔧 请求Token URL:', url);
console.log('🔧 请求参数:', {
grant_type: 'authorization_code',
code: code,
client_id: this.config.clientId,
redirect_uri: this.config.redirectUri
});
try {
const response = await fetch(url, {
method: 'POST',
@@ -91,15 +104,28 @@ export class OAuthClient {
body: data
});
console.log('🔧 Token响应状态:', response.status, response.statusText);
if (!response.ok) {
const errorData = await response.json();
console.error('获取访问令牌失败:', errorData);
console.error('获取访问令牌失败:', {
status: response.status,
statusText: response.statusText,
errorData: errorData
});
return null;
}
return await response.json() as TokenResponse;
const tokenResponse = await response.json() as TokenResponse;
console.log('✅ 获取访问令牌成功:', {
token_type: tokenResponse.token_type,
expires_in: tokenResponse.expires_in,
scope: tokenResponse.scope
});
return tokenResponse;
} catch (error) {
console.error('获取访问令牌网络错误:', error);
console.error('获取访问令牌网络错误:', error);
return null;
}
}
@@ -202,7 +228,17 @@ export class OAuthClient {
*/
generateState(): string {
// 获取当前端口号,优先级:API_PORT_CONFIG > PORT > 默认值
const currentPort = process.env.API_PORT_CONFIG || process.env.PORT;
let currentPort = process.env.API_PORT_CONFIG || process.env.PORT;
// 如果环境变量中没有端口号,尝试从浏览器location获取
if (!currentPort && typeof window !== 'undefined') {
currentPort = window.location.port;
}
// 如果仍然没有端口号,使用默认端口
if (!currentPort) {
currentPort = '51703'; // 默认端口
}
const randomStr = Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15);