我有一个 firebase 项目,我可以在其中使用 google 登录。现在我想在应用脚本中调用 firebase API 来创建 Gmail 插件。我可以使用 UrlFetchApp.fetch 在请求标头中调用不需要 Id 令牌的 API,但我也想调用在请求标头中包含 Id 令牌的 API。我使用下面的方法来获取firebase idToken但得到以下错误
[20-09-18 23:15:57:291 PDT] {
"error": {
"code": 400,
"message": "INVALID_IDP_RESPONSE : Invalid Idp Response: access_token audience is not for this project",
"errors": [
{
"message": "INVALID_IDP_RESPONSE : Invalid Idp Response: access_token audience is not for this project",
"domain": "global",
"reason": "invalid"
}
]
}
}
function getGoogleIDToken()
{
var firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
};
var res = UrlFetchApp.fetch('https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key='+firebaseConfig.apiKey, {
method: 'POST',
payload: JSON.stringify({
requestUri: 'https://'+firebaseConfig.authDomain,
postBody: 'access_token='+ScriptApp.getOAuthToken()+'&providerId=google.com',
returnSecureToken: true,
returnIdpCredential: true
}),
contentType: 'application/json',
muteHttpExceptions: true
});
Logger.log(res);
var responseData = JSON.parse(res);
idToken = responseData.idToken;
Logger.log('Google ID Token: ');
Logger.log(idToken);
return idToken;
}