You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Firebase ID令牌兑换为Google访问令牌时出现'invalid_client'错误

解决Firebase ID令牌兑换Google访问令牌时的"invalid_client"错误

核心错误原因

你当前的令牌兑换逻辑有误:使用urn:ietf:params:oauth:grant-type:jwt-bearer授权类型时,assertion参数要求传入服务账号生成的JWT,而非Firebase ID令牌。Firebase ID令牌的受众(aud)是你的Firebase项目,并非Google OAuth服务器,因此会被判定为无效客户端。

针对性解决方案

方案1:服务账号模拟用户获取令牌(Google Workspace场景)

如果你的用户属于Google Workspace域名,可通过服务账号的域级委托权限模拟用户身份获取访问令牌:

  1. 配置服务账号权限

    • 进入Google Cloud控制台的「IAM & Admin > 服务账号」,找到你的服务账号,勾选「启用G Suite域级委托」
    • 记录服务账号详情页的「客户端ID」
    • 登录Google Workspace Admin控制台,进入「安全 > API控制 > 域级委托」,添加服务账号的客户端ID,并授权作用域https://www.googleapis.com/auth/calendar
  2. 修改服务器代码
    使用Google官方的googleapis库生成模拟用户的JWT并兑换令牌:

const { google } = require('googleapis');
const admin = require('firebase-admin');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
});

// 初始化Google Auth客户端
const authClient = new google.auth.JWT(
  serviceAccount.client_email,
  null,
  serviceAccount.private_key,
  ['https://www.googleapis.com/auth/calendar'],
  null // 初始不指定模拟用户,后续动态设置
);

app.post("/exchange-token", async (req, res) => {
  const { firebaseIdToken } = req.body;

  try {
    // 验证Firebase令牌,提取用户邮箱
    const decodedToken = await admin.auth().verifyIdToken(firebaseIdToken);
    const userEmail = decodedToken.email;

    // 设置要模拟的用户邮箱(必须属于你的Google Workspace域名)
    authClient.subject = userEmail;

    // 获取有效访问令牌
    const { token } = await authClient.getAccessToken();

    res.status(200).json({ googleAccessToken: token });
  } catch (error) {
    console.error(
      "Token exchange error:",
      error.response ? error.response.data : error.message
    );
    res.status(500).json({ error: "Token exchange failed" });
  }
});

方案2:客户端直接获取访问令牌(普通Google账号场景)

如果用户是普通Google账号,需先在客户端完成Google OAuth授权,再将访问令牌或授权码传到服务器:

  • 客户端使用Google Sign-In SDK获取授权码,传给服务器后兑换访问令牌
  • 或客户端关联Firebase账号与Google账号,调用firebase.auth().currentUser.getIdTokenResult()获取包含OAuth凭证的令牌(需确保用户已授权对应作用域)

额外排查要点

  • 确认Firebase项目与Google Cloud项目为同一项目
  • 检查服务账号的private_key和client_email无复制错误(注意换行符和引号)
  • 验证作用域拼写完全正确,无多余空格或字符
  • 确认服务器网络可正常访问oauth2.googleapis.com

内容的提问来源于stack exchange,提问作者Mohammad Marouful Islam Zishan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 20:17:21