[GH-ISSUE #59] gemini-cli-oauth方式下,GeminiApiService的initializeAuth()逻辑是不是有问题 #49

Closed
opened 2026-02-27 07:17:40 +03:00 by kerem · 1 comment
Owner

Originally created by @airlulu on GitHub (Sep 29, 2025).
Original GitHub issue: https://github.com/justlovemaki/AIClient-2-API/issues/59

v1.0.0

        const credPath = this.oauthCredsFilePath || path.join(os.homedir(), CREDENTIALS_DIR, CREDENTIALS_FILE);
        try {
            const data = await fs.readFile(credPath, "utf8");
            const credentials = JSON.parse(data);
            this.authClient.setCredentials(credentials);
            console.log('[Auth] Authentication configured successfully from file.');
            if (forceRefresh) {
                console.log('[Auth] Forcing token refresh...');
                const { credentials: newCredentials } = await this.authClient.refreshAccessToken();
                this.authClient.setCredentials(newCredentials);
                await fs.writeFile(credPath, JSON.stringify(newCredentials, null, 2));
                console.log('[Auth] Refreshed token saved.');
            }
        } catch (error) {
            if (error.code === 'ENOENT') {
                console.log(`[Auth] Credentials file '${credPath}' not found. Starting new authentication flow...`);
                const newTokens = await this.getNewToken(credPath);
                this.authClient.setCredentials(newTokens);
                console.log('[Auth] New token obtained and loaded into memory.');
            } else {
                console.error('[Auth] Failed to initialize authentication from file:', error);
                throw new Error(`Failed to load OAuth credentials.`);
            }
        }

在异常捕获块中才会走getNewToken()的浏览器授权过程,也就是说如果想走浏览器授权方式,要给启动参数--gemini-oauth-creds-file传入一个不存在的凭证文件?
不知道我理解对不对,我是分析完代码,然后通过上面的方式才成功使用~

Originally created by @airlulu on GitHub (Sep 29, 2025). Original GitHub issue: https://github.com/justlovemaki/AIClient-2-API/issues/59 v1.0.0 ``` const credPath = this.oauthCredsFilePath || path.join(os.homedir(), CREDENTIALS_DIR, CREDENTIALS_FILE); try { const data = await fs.readFile(credPath, "utf8"); const credentials = JSON.parse(data); this.authClient.setCredentials(credentials); console.log('[Auth] Authentication configured successfully from file.'); if (forceRefresh) { console.log('[Auth] Forcing token refresh...'); const { credentials: newCredentials } = await this.authClient.refreshAccessToken(); this.authClient.setCredentials(newCredentials); await fs.writeFile(credPath, JSON.stringify(newCredentials, null, 2)); console.log('[Auth] Refreshed token saved.'); } } catch (error) { if (error.code === 'ENOENT') { console.log(`[Auth] Credentials file '${credPath}' not found. Starting new authentication flow...`); const newTokens = await this.getNewToken(credPath); this.authClient.setCredentials(newTokens); console.log('[Auth] New token obtained and loaded into memory.'); } else { console.error('[Auth] Failed to initialize authentication from file:', error); throw new Error(`Failed to load OAuth credentials.`); } } ``` 在异常捕获块中才会走getNewToken()的浏览器授权过程,也就是说如果想走浏览器授权方式,要给启动参数--gemini-oauth-creds-file传入一个不存在的凭证文件? 不知道我理解对不对,我是分析完代码,然后通过上面的方式才成功使用~
kerem closed this issue 2026-02-27 07:17:41 +03:00
Author
Owner

@justlovemaki commented on GitHub (Sep 30, 2025):

const credPath = this.oauthCredsFilePath || path.join(os.homedir(), CREDENTIALS_DIR, CREDENTIALS_FILE);
这个逻辑不是说先看oauthCredsFilePath是否有值 ,没有再从默认路径读取吗?你是配置文件oauthCredsFilePath有值吧?启动参数参数优先级更高,所以才能成功。

<!-- gh-comment-id:3349770634 --> @justlovemaki commented on GitHub (Sep 30, 2025): const credPath = this.oauthCredsFilePath || path.join(os.homedir(), CREDENTIALS_DIR, CREDENTIALS_FILE); 这个逻辑不是说先看oauthCredsFilePath是否有值 ,没有再从默认路径读取吗?你是配置文件oauthCredsFilePath有值吧?启动参数参数优先级更高,所以才能成功。
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/AIClient-2-API-justlovemaki#49
No description provided.