获取Gmail附加组件中的ID令牌以进行后端服务身份验证

Pat*_*ský 6 google-api google-openid google-apps-script google-oauth gmail-addons


我正在使用Google Apps脚本创建Gmail插件的背景.通过这个插件,我想使用REST服务请求连接到我的后端服务器(非Google服务).该请求必须经过授权.授权后,我可以向该服务器发出请求,以接收与该数据库中该用户相关的数据.我已经在我的webapp中使用Google登录来登录后端服务 - 在前端,我在授权响​​应中收到了GoogleUser对象内部的id_token.

问题是
我需要这个id_token才能通过Gmail插件连接到我的后端服务.但是,我找不到如何访问令牌的方法. 我认为必须通过Apps脚本中的API提供令牌

的研究
.
在webapp中,我使用Google Auth API收到id_token,如下所示:

Promise.resolve(this.auth2.signIn())
        .then((googleUser) => {
            let user_token = googleUser.getAuthResponse().id_token; // this is the id_token I need in the Gmail plugin, too

            // send the id_token to the backend service
            ...
        };
Run Code Online (Sandbox Code Playgroud)

在Google Apps脚本API中,我只能找到OAuth令牌:

ScriptApp.getOAuthToken();
Run Code Online (Sandbox Code Playgroud)

我假设令牌也可以存储在会话中.Google Apps脚本API包含Session类,它本身包含getActiveUser方法,该方法返回User对象.但是,User对象仅包含用户的电子邮件地址,没有id令牌(或其他任何内容):

Session.getActiveUser().getEmail();
Run Code Online (Sandbox Code Playgroud)


问题
是否有办法获取身份令牌?
我是否选择使用Gmail中已登录用户的数据登录后端服务器的正确方法?

Kos*_*Kos 6

方法一:使用getIdentityToken()

\n\n

获取有效用户的 OpenID Connect 身份令牌:

\n\n
var idToken = ScriptApp.getIdentityToken();\nvar body = idToken.split(\'.\')[1];\nvar decoded = Utilities.newBlob(Utilities.base64Decode(body)).getDataAsString();\nvar payload = JSON.parse(decoded);\nvar profileId = payload.sub;\nLogger.log(\'Profile ID: \' + profileId);\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

方法 2:使用 Firebase 和getOAuthToken()

\n\n

从 Apps 脚本的 OAuth 令牌获取 Google ID 令牌的步骤:

\n\n
    \n
  1. 为您的 Apps 脚本项目启用 Identity Toolkit API。
  2. \n
  3. 将新的 Firebase 项目添加到现有的 Google Cloud Platform 项目:https://console.firebase.google.com/
  4. \n
  5. 为平台创建 Firebase 应用程序:Web
  6. \n
  7. 您将获得您的配置数据:var firebaseConfig = {apiKey: YOUR_KEY, ...}
  8. \n
  9. 为您的 Firebase 项目启用 Google 登录方法,网址为https://console.firebase.google.com/project/PROJECT_ID/authentication/providers
  10. \n
  11. 使用Apps Script函数获取当前用户的ID Token:
  12. \n
\n\n
\n\n
function getGoogleIDToken()\n{\n    // get your configuration from Firebase web app\'s settings\n    var firebaseConfig = {\n        apiKey: "***",\n        authDomain: "*.firebaseapp.com",\n        databaseURL: "https://*.firebaseio.com",\n        projectId: "***",\n        storageBucket: "***.appspot.com",\n        messagingSenderId: "*****",\n        appId: "***:web:***"\n    };\n\n    var res = UrlFetchApp.fetch(\'https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key=\'+firebaseConfig.apiKey, {\n        method: \'POST\',\n        payload: JSON.stringify({\n            requestUri: \'https://\'+firebaseConfig.authDomain,\n            postBody: \'access_token=\'+ScriptApp.getOAuthToken()+\'&providerId=google.com\',\n            returnSecureToken: true,\n            returnIdpCredential: true\n        }),\n        contentType: \'application/json\',\n        muteHttpExceptions: true\n    });\n\n    var responseData = JSON.parse(res);\n\n    idToken = responseData.idToken;\n\n    Logger.log(\'Google ID Token: \');\n    Logger.log(idToken);\n\n    return idToken;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

感谢Ri\xc3\xabl Notermans

\n