firebasegoogle-apps-scriptgoogle-cloud-platformhttp-status-code-400audience

How to user firebase API having auth in request header in app script gmail add on project


I have a firebase project in which I enabled to sign in with google. Now I want to call the firebase APIs in app script for creating Gmail add on. I am able to call APIs which do not require Id token in request header using UrlFetchApp.fetch but I also want to call APIs which contain Id token in request header. I used the below method to get firebase idToken but getting below error

[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;
}

Solution