[HOWTO] Get FirebaseID TOKEN from Apps Script OAuth Token!

164 views
Skip to first unread message

Riël Notermans

unread,
Aug 6, 2019, 10:08:01 AM8/6/19
to Google Apps Script Community
If you want to do authenticated calls to Firebase or to Cloudfunctions that would need a token that is created on the client,
this is a way you can fetch the token (Firebase idToken) from your OAuthToken from Apps Script.
This is handy in many cases where the script is run as the user (like container bound).


1) Add firebase scope to manifest
2) Use this code to fetch the idToken 
3) Make sure the apps script is bound to the firebase project (<-- yes, this was the main key)

function getFBIdToken() {
  var token = ScriptApp.getOAuthToken();

  var url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key=[FIREBASE KEY]"
  var data =  {"postBody":"access_token="+token+"&providerId=google.com","requestUri":"https://[firebaseproject].firebaseapp.com","returnIdpCredential":true,"returnSecureToken":true};
  var options = {
    method: "POST",
    payload: JSON.stringify(data),
    muteHttpExceptions: true,
    contentType: 'application/json'
  }

  var data = JSON.parse(UrlFetchApp.fetch(url,options).getContentText());
  return data.idToken;
}

Enjoy!

Riël Notermans

unread,
Feb 4, 2020, 5:35:35 PM2/4/20
to Google Apps Script Community
It needs the "https://www.googleapis.com/auth/userinfo.email" scope and the "https://www.googleapis.com/auth/firebase" scope.

Met vriendelijke groet,

pic
Riël Notermans
Technisch Directeur
logo
phone
email
email
040 711 41 94
ri...@zzapps.nl
www.zzapps.nl

Andrew Apell

unread,
Feb 5, 2020, 12:21:36 PM2/5/20
to Google Apps Script Community
Thanks for this...
Reply all
Reply to author
Forward
0 new messages