Firebase Generate New Private Key

Private
CONNECTORS TO OTHER SERVICES‎ > ‎Firebase‎ > ‎Tutorials‎ > ‎

Authenticate with a Service Account

Firebase legacy tokens are the easiest way to authenticate to your Firebase Realtime Database but are deprecated and not recommended by Google. They are still working but it is recommended to move to an authentication via Google OAuth2 access tokens.
If your Apps Script function is running as yourself, it is easier and more secure to authenticate via the default Apps Script access token. But if your script is deployed as an add-on or a web app executed by the user, you need to use a Service Account to generate a valid access token.
To generate an access token from a Service Account, best is to rely on the official OAuth2 for Apps Script library. You can then easily reuse the token in the getDatabaseByUrl() method of FirebaseApp:
// generate an access token from a Service Account
var token = getFirebaseService().getAccessToken();
// authenticated request to Firebase
var fb = FirebaseApp.getDatabaseByUrl('https://my_db.firebaseio.com/', token);
}

Generate a new private key

A Service Account is automatically created for all Firebase projects. You need to retrieve the email address of your service account and a private key linked to that account.
Open your project in the Firebase Console and click on 'Project settings', then select the 'SERVICE ACCOUNTS' tab.

The service account email address (or client email) is directly visible. To retrieve the private key, click on the 'Generate New Private Key' button at the bottom of the page.

This will prompt you to download a JSON file on your computer. Open it with a text editor and copy the private key.

Install the OAuth2 library in your script

Here's a copy of the installation steps listed in the documentation of this library:
https://github.com/googlesamples/apps-script-oauth2#setup
  1. Click on the menu item 'Resources > Libraries.'
  2. In the 'Add a Library' text box, enter the script ID 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF and click the 'Select' button.
  3. Choose a version in the dropdown box (usually best to pick the latest version).
  4. Click the 'Save' button.

Generate a valid OAuth2 access token from your service account

As indicated in the Firebase documentation, here are the required scopes to create a valid access token for Firebase:
  • https://www.googleapis.com/auth/userinfo.email
  • https://www.googleapis.com/auth/firebase.database
The OAuth2 library provides a code sample for the service account authorization flow available here:
Here's the code sample adapted to our use case:
var PRIVATE_KEY = '-----BEGIN PRIVATE KEY-----n.n-----END PRIVATE KEY-----n';
var CLIENT_EMAIL = '.'; // service account email address
/**
* Authorizes and makes a request to the Firebase Realtime Database.
function run(userId, newUserData) {
if (service.hasAccess()) {
var fb = FirebaseApp.getDatabaseByUrl('https://my_db.firebaseio.com/', service.getAccessToken());
} else {
}

* Reset the authorization state, so that it can be re-tested.
function reset() {
service.reset();

* Configures the service.
function getFirebaseService() {
// Set the endpoint URL.
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
// Set the private key and issuer.
.setIssuer(CLIENT_EMAIL)
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getScriptProperties())
// Set the scopes.
.setScope('https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/firebase.database');

That's it. The OAuth2 library will generate a valid access token, save it under your Script Properties and refresh it when necessary. As stated in the Firebase documentation, the private key of your service account should never be shared. In the example above, it is saved directly as a global variable in the source code but you can decide to store it somewhere else (eg: in a Script Property).

Firebase Generate New Private Key Mac

What is a Firebase Server Key? A Firebase Server Key and Firebase Sender ID are required in order to send push notifications to Android mobile app devices. The goal of this section is to provision your Firebase Server Key and Firebase Sender ID for use in OneSignal. Then go to the ‘service accounts’ tab, select python and then click on the ‘Generate new private key button‘. Bitcoin private key with balance generator. NOTE: THIS STEP IS GOING TO GENERATE A PRIVATE KEY, USED TO ACCESS YOUR DATABASE. YOU HAVE TO KEEP THIS KEY CONFIDENTIAL AT ALL TIMES, BECAUSE ANYONE HAVING THIS PRIVATE KEY WILL BE ABLE TO MODIFY YOUR DATABASE. If you lose your app signing key, you can generate a new upload key and register it with Google to continue updating your app. Mighty mouse mac download free. If you generated a new upload key: Use your new upload key to sign releases before you upload them to Google Play. After you upload them, Google checks the release for an upload key to verify your identity. Jan 02, 2020 Get Google Cloud Account Credentials from Firebase. You can Generate New Private Key from Project Settings from Firebase Console. After that you need to copy the databaseURL for initiating the App. Usage Examples. This is the first step that you need to do before doing any other actions. The OAuth2 library will generate a valid access token, save it under your Script Properties and refresh it when necessary. As stated in the Firebase documentation, the private key of your service account should never be shared.