【发布时间】:2019-10-19 05:17:18
【问题描述】:
我将电子表格用作 Firebase 应用的管理界面,我可以在其中授权应用上的一些用户交易。该电子表格利用了 Google 应用脚本和云功能
我能够在电子表格和我的后端来回发送数据,现在,下一步是锁定对云函数 HTTP URL 的访问权限,授权用户将自定义声明中的 admin 属性设置为 true。
为此,我希望将从 Google Apps Script API (ScriptApp.getOAuthToken()) 获取的用户 OAuth 令牌作为请求负载的一部分发送,并使用 firebase rest API 方法 https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key=[API_KEY] 获取用户的 id 令牌和other 中的其他凭据,用于验证和授权管理员用户和事务。
const credentials = {
postBody: `id_token=${token}&providerId=google.com`,
requestUri: oAuthCredentials.web.redirect_uris[0],
returnIdpCredential: false,
returnSecureToken: true
}
APIRequest(IdentityUrls.signInWithOAuth, {
headers: {
'Content-Type': "application/json"
},
method: 'POST',
body: JSON.stringify(credentials)
}, (error, res) => {
...// perform actions here
})
问题是我不断收到 INVALID_IDP_RESPONSE:提供的身份验证凭据格式错误或已过期。我不知道为什么会这样,希望能得到帮助
【问题讨论】:
-
@firebase support/team 我需要一些帮助
-
我终于想通了。我现在正在做的是将问题中描述的 OAuth 令牌发送到后端,并向token info 端点发出 POST 请求,有效载荷为
access_token:OAuth token。这会发送包含用户电子邮件、email_verified、expiry_date 等的响应。然后使用此用户电子邮件,我可以在 Firebase Admin SDK 上获取 userRecord,它公开了一个 customClaims 属性。阅读有关解决方案的更多信息here
标签: firebase google-apps-script google-cloud-functions google-oauth