简体   繁体   English

使用 getIdToken 作为 Google Sheet API 的授权令牌

[英]Use getIdToken as Authorization token for Google Sheet API

I am trying to make a call to: https://sheets.googleapis.com/v4/spreadsheets/GOOGLE_SHEET_ID:batchUpdate我正在尝试致电: https://sheets.googleapis.com/v4/spreadsheets/GOOGLE_SHEET_ID:batchUpdate

as in:如:

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: "*",
              },
            },
          ],
        }),
      }
    );

I am trying to use firebase auth to get the bearer token as follows:我正在尝试使用 firebase auth 来获取不记名令牌,如下所示:

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

The problem is that using that token I get this response (yes I checked that the bearerToken is properly set):问题是使用该令牌我得到了这个响应(是的,我检查了 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"
  }
}

Is the Id Token valid? Id Token 有效吗? How can I get the appropiate Access Token from Firebase Auth?如何从 Firebase Auth 获取适当的访问令牌?

If you want to call the google sheets API, then you need to get access token from the Google Authorization server.如果你想调用谷歌表格 API,那么你需要从谷歌授权服务器获取访问令牌。 Is firebase indirectly getting the bearer token from the Google Authorization server? firebase 是否间接从 Google 授权服务器获取不记名令牌? Secondly, bearer token and ID token are two separate concepts.其次,不记名令牌和身份令牌是两个独立的概念。 Oauth ID Token is used to get information about User but access token can be passed to Resource API. Oauth ID 令牌用于获取有关用户的信息,但访问令牌可以传递给资源 API。 I suspect that you are passing a totally unrelated token to the API.我怀疑您正在将一个完全不相关的令牌传递给 API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有授权标头(session.getIdToken()。getJwtToken())的AWS API调用 - AWS API Call with Authorization Header ( session.getIdToken().getJwtToken() ) 使用 Google 表格 API 刷新令牌对表格单元格使用更新方法 - Using Google Sheets API refresh token to use update method for Sheet cell Firebase .getIdToken() 返回无效令牌 - Firebase .getIdToken() returns invalid token AngularFire getIdToken(true)不刷新令牌 - AngularFire getIdToken(true) not refreshing token Google API授权失败 - Google API authorization failing 如何在 Google Sheet 上的 Apps Scripts 中使用 google API? - How to use google API in Apps Scripts on Google Sheet? 如何使用 Google Sheet 上的信息在远程服务器上调用 PATCH API - How to use the information on a Google Sheet to call a PATCH API on a remote server firebase版本9中的getIdToken()认证方法如何使用? - How to use the getIdToken() auth method in firebase version 9? 无法在 Angular 中使用授权令牌调用 API - Not able to call API with Authorization Token in Angular Firebase currentUser.getIdToken() 在 signInWithCustomToken 之后返回自定义令牌 - Firebase currentUser.getIdToken() returns custom token after signInWithCustomToken
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM