惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Martin Fowler
Martin Fowler
雷峰网
雷峰网
IT之家
IT之家
小众软件
小众软件
M
MIT News - Artificial intelligence
博客园 - 聂微东
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
C
Check Point Blog
云风的 BLOG
云风的 BLOG
腾讯CDC
H
Help Net Security
Y
Y Combinator Blog
I
InfoQ

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(google): refresh Gemini CLI OAuth tokens · openclaw/o...
jason-allen- · 2026-05-17 · via Recent Commits to openclaw:main

@@ -4,22 +4,11 @@ import { resolveGoogleOAuthIdentity, resolveGooglePersonalOAuthIdentity } from "

44

import { isGeminiCliPersonalOAuth } from "./oauth.settings.js";

55

import { REDIRECT_URI, TOKEN_URL, type GeminiCliOAuthCredentials } from "./oauth.shared.js";

667-

export async function exchangeCodeForTokens(

8-

code: string,

9-

verifier: string,

10-

): Promise<GeminiCliOAuthCredentials> {

11-

const { clientId, clientSecret } = resolveOAuthClientConfig();

12-

const body = new URLSearchParams({

13-

client_id: clientId,

14-

code,

15-

grant_type: "authorization_code",

16-

redirect_uri: REDIRECT_URI,

17-

code_verifier: verifier,

18-

});

19-

if (clientSecret) {

20-

body.set("client_secret", clientSecret);

21-

}

22-7+

async function requestTokenGrant(body: URLSearchParams): Promise<{

8+

access_token?: string;

9+

refresh_token?: string;

10+

expires_in?: number;

11+

}> {

2312

const response = await fetchWithTimeout(TOKEN_URL, {

2413

method: "POST",

2514

headers: {

@@ -35,26 +24,110 @@ export async function exchangeCodeForTokens(

3524

throw new Error(`Token exchange failed: ${errorText}`);

3625

}

372638-

const data = (await response.json()) as {

39-

access_token: string;

40-

refresh_token: string;

41-

expires_in: number;

27+

return (await response.json()) as {

28+

access_token?: string;

29+

refresh_token?: string;

30+

expires_in?: number;

4231

};

32+

}

433344-

if (!data.refresh_token) {

45-

throw new Error("No refresh token received. Please try again.");

34+

async function buildGeminiCliCredentials(params: {

35+

tokenResponse: {

36+

access_token?: string;

37+

refresh_token?: string;

38+

expires_in?: number;

39+

};

40+

refreshTokenFallback?: string;

41+

existing?: Pick<GeminiCliOAuthCredentials, "email" | "projectId">;

42+

}): Promise<GeminiCliOAuthCredentials> {

43+

const accessToken = params.tokenResponse.access_token;

44+

if (!accessToken) {

45+

throw new Error("No access token received. Please try again.");

46+

}

47+48+

let identity: { email?: string; projectId?: string } = params.existing ?? {};

49+

try {

50+

if (!identity.email || !identity.projectId) {

51+

const discovered = await resolveGeminiCliIdentity(accessToken);

52+

identity = {

53+

email: identity.email ?? discovered.email,

54+

projectId: identity.projectId ?? discovered.projectId,

55+

};

56+

}

57+

} catch {

58+

// If identity discovery is temporarily unavailable during refresh, keep the

59+

// already-stored identity binding instead of failing token renewal.

4660

}

476148-

const identity = isGeminiCliPersonalOAuth()

49-

? await resolveGooglePersonalOAuthIdentity(data.access_token)

50-

: await resolveGoogleOAuthIdentity(data.access_token);

51-

const expiresAt = Date.now() + data.expires_in * 1000 - 5 * 60 * 1000;

62+

const expiresInMs =

63+

typeof params.tokenResponse.expires_in === "number"

64+

? params.tokenResponse.expires_in * 1000

65+

: 0;

66+

const expiresAt = Date.now() + expiresInMs - 5 * 60 * 1000;

52675368

return {

54-

refresh: data.refresh_token,

55-

access: data.access_token,

69+

refresh: params.tokenResponse.refresh_token ?? params.refreshTokenFallback ?? "",

70+

access: accessToken,

5671

expires: expiresAt,

5772

projectId: identity.projectId,

5873

email: identity.email,

5974

};

6075

}

76+77+

async function resolveGeminiCliIdentity(

78+

accessToken: string,

79+

): Promise<{ email?: string; projectId?: string }> {

80+

return isGeminiCliPersonalOAuth()

81+

? await resolveGooglePersonalOAuthIdentity(accessToken)

82+

: await resolveGoogleOAuthIdentity(accessToken);

83+

}

84+85+

export async function exchangeCodeForTokens(

86+

code: string,

87+

verifier: string,

88+

): Promise<GeminiCliOAuthCredentials> {

89+

const { clientId, clientSecret } = resolveOAuthClientConfig();

90+

const body = new URLSearchParams({

91+

client_id: clientId,

92+

code,

93+

grant_type: "authorization_code",

94+

redirect_uri: REDIRECT_URI,

95+

code_verifier: verifier,

96+

});

97+

if (clientSecret) {

98+

body.set("client_secret", clientSecret);

99+

}

100+101+

const refreshed = await buildGeminiCliCredentials({

102+

tokenResponse: await requestTokenGrant(body),

103+

});

104+

if (!refreshed.refresh) {

105+

throw new Error("No refresh token received. Please try again.");

106+

}

107+

return refreshed;

108+

}

109+110+

export async function refreshTokensForGeminiCli(credentials: {

111+

refresh: string;

112+

email?: string;

113+

projectId?: string;

114+

}): Promise<GeminiCliOAuthCredentials> {

115+

const { clientId, clientSecret } = resolveOAuthClientConfig();

116+

const body = new URLSearchParams({

117+

client_id: clientId,

118+

grant_type: "refresh_token",

119+

refresh_token: credentials.refresh,

120+

});

121+

if (clientSecret) {

122+

body.set("client_secret", clientSecret);

123+

}

124+125+

return await buildGeminiCliCredentials({

126+

tokenResponse: await requestTokenGrant(body),

127+

refreshTokenFallback: credentials.refresh,

128+

existing: {

129+

email: credentials.email,

130+

projectId: credentials.projectId,

131+

},

132+

});

133+

}