i want to configure firebase realtime databse, with my firebase cloud function.
1)what is "serviceAccountKey.json"?
2) How to get this file?
3) did one "serviceAccountKey.json" is enough for all the users using firebase functions?
suppose if i use below sample
"exports.dbCreate = functions.database.ref('/path').onCreate((event) => {
const createdData = event.data.val(); // data that was created
});"
how to call dbCreate and what path i have to give in "/path"?
how to get serviceAccountKey.json?
i am using the versions " "actions-on-google" :"^2.0.1",
"firebase-admin": "~5.12.0",
"firebase-functions": "^1.0.1",
"dialogflow": "^0.1.0",
"dialogflow-fulfillment": "^0.4.1","
Firebase Functions are server-side (cloud based) scripts that interact with your Firebase project. So, they need a key since they have full access to your project.
Your example functions.database.ref('/path').onCreate is not something you call, it is triggered.
Get started with
a service account to communicate with the Firebase service, and a
configuration file with your service account's credentials.
If you don't already have a Firebase project, add one in the Firebase console. The Add
project dialog also gives you the option to add Firebase to an
existing Google Cloud Platform project.
Navigate to the Service Accounts
tab in your project's settings page.
Click the Generate New Private Key button at the bottom of the Firebase Admin SDK section of the Service Accounts tab.
Related
When initializing Firebase in Nodejs project, why we include apikey,databaseurl,storageBucket,authDomain, appId, messagingSenderId, projectId in Firebase.initializeApp()? Without some of these properties, it is still working, then why do we need to pass it?
And also for security, we use Firebase Authentication for checking user auth.uid, so why do we need to pass the above properties in initializeApp()?
Does passing these properties, Firebase will check it by default? If Firebase checks it by default, we don't need Firebase Authentication then?
I'm a beginner kindly help.
Firebase consists of >18 products these days, and many of them take different configuration data at startup. But since you only call initializeApp once for all these products, you have to pass the configuration data for all products in this one call.
That's why all example in the Firebase documentation and console show how to pass all possible configuration data. Depending on the products you actually use, and the platform you run on, you may need fewer of these values, but including all of them never causes problems.
When you register an app with a Firebase project, the Firebase console provides a Firebase configuration file (Apple/Android apps) or a configuration object (web apps) that you add directly to your local app directory.
That is, a Firebase config file / object associates an app with a specific Firebase project and its resources. It consists of unique and non-secret identifiers for your project. A Firebase config file generally consists of apiKey, databaseURL, projectId, storageBucket, messagingSenderId, appId, measurementId.
These parameters are required by Firebase and Google services to communicate with Firebase server APIs and to associate client data with the Firebase project and Firebase app. The apiKey and the projectId are the mandatory fields in the configuration file/object. And, other fields are optional. Each of the other fields corresponds to an optional part of Firebase.
This is because Firebase contains many services/products such as realtime nosql database services, blob storage, push notifications/messaging, and ofcourse Authentication among many more things.
If you do not desire to use the other parts of Firebase, simply do not reference them nor enable them. It is completely fine to only use Firebase Authentication.
The content of the Firebase config file or object is considered public, including the app's platform-specific ID (Apple bundle ID or Android package name) and the Firebase project-specific values, like the API Key, project ID, Realtime Database URL, and Cloud Storage bucket name. Given this, it is recommended to use Firebase Security Rules to protect your data and files in Realtime Database, Cloud Firestore, and Cloud Storage.
I would like to test the Google Cloud Speech-to-Text API from within Firebase Emulators. I currently have a trigger set on Firebase Storage that automatically gets fired when I upload a file via the Emulator Storage UI. This makes a request to the Speech to Text API, but I keep getting a permission denied error, as follows:
Error: 7 PERMISSION_DENIED: Cloud Speech-to-Text API has not been used in project 563584335869 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/speech.googleapis.com/overview?project=563584335869 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
I understand that project 563584335869 is the Firebase Cli project.
I have set the following environment variables when starting the emulator:
export GCLOUD_PROJECT=my-actual-glcloud-project-id && export FIREBASE_AUTH_EMULATOR_HOST='localhost:9099' && export GOOGLE_APPLICATION_CREDENTIALS=./path/to/service-account.json &&
firebase emulators:start
The service_account.json key file is associated with a service_account that has the following roles, as demonstrated by running
gcloud projects get-iam-policy my_project_id --flatten="bindings[].members" --format='table(bindings.role)' --filter="bindings.members:serviceAccount:my_service_account#my_project_id.iam.gserviceaccount.com"
ROLE
roles/speech.admin
roles/storage.admin
roles/storage.objectAdmin
roles/storage.objectCreator
roles/storage.objectViewer
Since the credentials for the service account I am using should have admin access to the speech to text api, why do I keep getting a permission denied error when running from the emulator, and how can I fix it?
The project id 563584335869 is not yours. It is firebase-cli cloud project’s project-id. In this case, the problem is arising because you have to set your own configuration using your credentials or your key.
You can see below a code for NodeJS which I found in github[1] where it shows how to configure your authentication to use the API.
// Imports the Google Cloud client library
const textToSpeech = require('#google-cloud/text-to-speech');
// Create the auth config
const config = {
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
};
// Creates a client
const client = new textToSpeech.TextToSpeechClient(config);
[1]https://github.com/googleapis/nodejs-text-to-speech/issues/26
EDIT
There are different ways to set up your authentication for speech to text. One way to resolve this problem would be to add the same auth configuration as the Text-to-Speech and it should look something like this in your code.
// Imports the Google Cloud client library
const speech = require('#google-cloud/speech');
// Create the auth config
const authconfig = {
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
};
// Creates a client
const client = new speech.SpeechClient(authconfig);
Another way to solve this problem according to this Google Cloud Documentation[2] is to setup your authentication.
[2]https://cloud.google.com/speech-to-text/docs/libraries#setting_up_authentication
We've recently created a GCP project and a Cloud Storage bucket in it.
Then we decided to use Firebase to host our React app. However, after creating the Firebase project (Why did we need to create a new project?), we got a different storage bucket. And it seems that it's not possible to view the same bucket from both projects.
Our backend uses a Cloud Storage bucket. Our frontend sends files to a Firestore bucket, which is different. How can we link it together?
It's important to know that a Firebase project IS a Google Cloud project. Read more about that in this blog. Firebase is essentially a layer of APIs and services in a Google Cloud project. What you probably should have done in the first place is add Firebase to your existing Cloud project, but it's probably not worthwhile to do that if you already have an existing Firebase project.
There's really no way to link the projects together that would be convenient for you. What I recommend you do is abandon your first Cloud project, and copy the contents of your first bucket into your Firebase project default bucket. Also read more about the relationship between Firebase and Google Cloud with respect to Cloud Storage in this blog.
You can use Firebase storage's getStorage function to specify the Google Cloud Storage Bucket. Then use gsutil to add the appropriate permissions between your Firebase project and the GCS bucket. You may need to edit the bucket permissions by going to Storage > Buckets and selecting Edit Bucket Permissions from the kebab menu (⋮) right of the bucket name. Here, you add can add the service-#gcp-sa-firebasestorage.iam.gserviceaccount.com
Frontend/browser Firebase code:
import { getApp } from "firebase/app";
import { getStorage } from "firebase/storage";
const firebaseApp = getApp();
const storage = getStorage(firebaseApp, "gs://my-custom-bucket");
command line to add permissions:
gsutil -m acl ch -r -u service-<project number>#gcp-sa-firebasestorage.iam.gserviceaccount.com gs://<my-custom-bucket>
The service-project number can be found in the Firebase project's console, under settings.
I have created/forked a lil Google Apps Script Library to manage Firebase Firestore and Firebase Remote Config called FirebaseGoogleAppsScript. The goal is to simply manage the contents of your collections in an apps script as well as update your remote config.
My issue is I can't get the a service account to do both.
Firebase creates two service accounts upon creating a project:
The first is listed in the Firebase Console -> Project Settings -> Service Accounts. This one I use within my cloud functions to retrieve the Remote Config just fine. However in the Apps Script Project it is unable to retrieve any data from firestore. I tried adding all kinds of roles including Owner and Editor yet no firestore data, but I still can get the RemoteConfig.
The second is only visible in the GCP service accounts and has the title: Firebase Admin SDK Service Agent with the roles Firebase Admin SDK Administrator Service Agent and Service Account Token Creator. This one is able to retrieve all the data from firestore within an Apps Script Project. However in the apps script project I can't get it to retrieve the RemoteConfig even if I add the role Firebase Remote Config Admin.
I have also made my own service account which was able to get the Remote config and just about everything else from Firebase except the Firestore data. Seems only the one service account created by Firebase is able to get any data.
To recreate the issue simply deploy my lil FirebaseGoogleAppsScript project and associate it to the same GCP project Firebase is connected to. There is a test file in it which can recreate the issue assuming you have some data in RemoteConfig and a collection called posts with some docs.
What the heck is going on here? Why can't I make a service account who can access Firestore and RemoteConfig? Any ideas on what to do to create a proper role to do both? Do I really have to use two separate service accounts?
Screenshot attached when I entered "Firebase deploy" command
I was trying to execute this set on instructions while deploying webhook using Google Cloud Functions for Firebase and the static resources needed by the project using Firebase Hosting:
Run firebase init, and select to configure Hosting and Functions. Select the project. In the configuration wizard, accept all the default choices.
Generate a private key using Firebase Settings/Service Accounts, and edit functions/database.js with the path to the JSON cert file. Now populate the database: node database.js
Run firebase deploy and take note of the endpoint where the fulfillment webhook has been published. It should look like Function URL (yourGame): https://us-central1-YOUR_PROJECT.cloudfunctions.net/yourGame. The command will also deploy the static assets at https://us-central1-YOUR_PROJECT.cloudfunctions.net/.
On running firebase deploy I can't find the "Function URL". It would just display "Hosting URL" and "Project Console".
A screenshot is attached upon running "firebase deploy" on CLI.
And also what does it mean by "Now populate the database: node database.js"?
Follow this Project Console URL.
You then should be in your project's dashboard, on the left side of the screen you gonna see a bunch of options, among them one called "Functions", click on it.
Your URL should be displayed there.
If you created a new Firebase project, then your index.js in your ./functions directory will look like this by default:
const functions = require('firebase-functions');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
//exports.helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello World!");
//});
Make sure you uncomment the hellowWorld function. Then save your project. Then run firebase deploy. Then, the url will show up in your dashboard.