【问题标题】:Use getIdToken as Authorization token for Google Sheet API使用 getIdToken 作为 Google Sheet API 的授权令牌
【发布时间】:2021-08-20 01:10:51
【问题描述】:

我正在尝试致电: https://sheets.googleapis.com/v4/spreadsheets/GOOGLE_SHEET_ID:batchUpdate

如:

fetch(
      `https://sheets.googleapis.com/v4/spreadsheets/GOOGLE_SHEET_ID:batchUpdate`,
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${bearerToken}`,
        },
        body: JSON.stringify({
          requests: [
            {
              repeatCell: {
                range: {
                  startColumnIndex: 0,
                  endColumnIndex: 1,
                  startRowIndex: 0,
                  endRowIndex: 1,
                  sheetId: 0,
                },
                cell: {
                  userEnteredValue: {
                    numberValue: 10,
                  },
                },
                fields: "*",
              },
            },
          ],
        }),
      }
    );

我正在尝试使用 firebase auth 来获取不记名令牌,如下所示:

firebase.auth().onAuthStateChanged(user => {
      if (user) {
        user.getIdToken().then(token => setBearerToken(token))
    }
 });

问题是使用该令牌我得到了这个响应(是的,我检查了 bearerToken 是否设置正确):

{
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

Id Token 有效吗?如何从 Firebase Auth 获取适当的访问令牌?

【问题讨论】:

    标签: javascript firebase google-sheets google-api


    【解决方案1】:

    如果你想调用google sheet API,那么你需要从谷歌授权服务器获取访问令牌。 firebase 是否间接从 Google 授权服务器获取不记名令牌?其次,不记名令牌和身份令牌是两个独立的概念。 Oauth ID 令牌用于获取有关用户的信息,但访问令牌可以传递给资源 API。我怀疑您正在向 API 传递一个完全不相关的令牌。

    【讨论】:

      猜你喜欢
      • 2016-10-13
      • 2015-04-24
      • 2012-05-06
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 2021-10-24
      相关资源
      最近更新 更多