How to supply authentication to firebase-import? - firebase

I would like to import a json file into my Firebase database. I tried firebase-import as follows:
$ firebase-import --firebase_url https://mytest.firebaseio.com/ --json test.json
but I am getting a "Permission denied" error.
I know that firebase-import has --auth option, which says "Specify an auth token to use (e.g. your Firebase Secret)". How do I get an auth token or my Firebase Secret?

Looks to me like you need to create a service account (see https://firebase.google.com/docs/server/setup) and the provide the service account JSON to firebase-import using the --service_account command-line option.

Create New Project.
Click Add App
Add Firebase to your web app.
this code will show up
<script>
// Initialize Firebase
// TODO: Replace with your project's customized code snippet
var config = {
**apiKey: "<API_KEY>",**
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
storageBucket: "<BUCKET>.appspot.com",
};
firebase.initializeApp(config);
</script>
that is you API key then add -a API_KEY
so you will have
firebase-import --firebase_url https://test.firebaseio-demo.com --json test.json --merge -a API_KEY

Related

FirebaseProjectNotFoundException: Firebase project id could not be found on this Firebase account

I found this project. After I added to index.html file the following Firebase configuration:
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js";
import { initializeApp } from "https://www.gstatic.com/firebasejs/8.6.1/firebase-auth.js";
import { initializeApp } from "https://www.gstatic.com/firebasejs/8.6.1/firebase-firestore.js";
import { initializeApp } from "https://www.gstatic.com/firebasejs/8.6.1/firebase-storage.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyDoO8efzrDVveeXvqqrc39D3XiqfsyoKeU",
authDomain: "diary-app-course-c6663.firebaseapp.com",
projectId: "diary-app-course-c6663",
storageBucket: "diary-app-course-c6663.appspot.com",
messagingSenderId: "1031493909898",
appId: "1:1031493909898:web:3cd71413645447b67c1c73"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
</script>
Next, I did the following steps on command line:
$ dart pub global activate flutterfire_cli
Warning: Pub installs executables into $HOME/.pub-cache/bin, which is not on your path.
You can fix that by adding this to your shell's config file (.bashrc, .bash_profile, etc.):
export PATH="$PATH":"$HOME/.pub-cache/bin"
$ vim ~/.bashrc
$ source ~/.bashrc
$ flutterfire configure
$ curl -sL https://firebase.tools | bash
Unfortunately, I got this error:
$ flutterfire configure
i Found 1 Firebase projects. Selecting project diary-app-course.
FirebaseProjectNotFoundException: Firebase project id "diary-app-course" could not be found on this Firebase account.
$ grep -R diary-app-course *
web/index.html: authDomain: "diary-app-course-c6663.firebaseapp.com",
web/index.html: projectId: "diary-app-course-c6663",
web/index.html: storageBucket: "diary-app-course-c6663.appspot.com",
What did I miss?
Follow these steps:
Remove if their any default project exists in .firebaserc file.
Before:
{
"projects": {
"default": "diary-app-course"
}
}
After
{
"projects": {
}
}
Run this command in Firebase CLI:
firebase logout
Log in again:
firebase login
Again run this command in Firebase CLI
flutterfire configure
Hope this works.
flutterfire configure --project=diary-app-course-c6663
You could see your project id on your firebase link
for example : https://console.firebase.google.com/u/0/project/ this is your project id/database/xxxx/data
So, I made some change: If you are trying to logging in one account for registering firebase while creating new project, and at that time but if your android studio account was another one it will be problem.
Make sure to use one gmail account for registering new project and login that entered project
LOG OUT and LOG IN again
Please choose default account and create

setting up environment variable in travis

I am building a react app.
In my react app I have environment variables which I am acessing using process.env
Something like this
firebase.initializeApp({
apiKey: process.env.REACT_APP_API_KEY,
authDomain: process.env.REACT_APP_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_DATABASE_URL,
projectId: process.env.REACT_APP_PROJECT_ID,
storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_MESSANGER_ID,
appId: process.env.REACT_APP_ID,
measurementId: process.env.REACT_APP_MEASUREMENT_ID,
});
I want these variables to have value when gatsby is making my production build , I am not sure if this is correct, but I have added them in settings in my travis. Is this correct? Will my react have access to above variables?
Update: They aren't accessible in travis with above approach. so can someone help me in comprehending on how to set react variables in travis?
I aslo want to deploy by static site it to firebase hosting, In the above image, the first name is firebase which contains my firebase token.
How can I access that token in my travis cli?
Update: Here is how my travis.yml looks like
language: node_js
node_js: -"node"
before_script: -"npm install firebase-tools -g"
-"npm install gatsby -g"
-"npm install"
script: -"npm run develop"
after_success: -"firebase deploy --token=${firebase}"

Error while initializing Service Account key

When I try to initialize my service account key like so:
const admin = require('firebase-admin');
const serviceAccount = require("./serviceAccountKey.json");
// Initialize the Firebase Storage admin constant
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "<DATABASE-NAME>.appspot.com",
databaseURL: "https://<DATABASE-NAME>.firebaseio.com"
});
I get the following error Error parsing triggers: Cannot find module './serviceAccountKey.json'
My serviceAccountKey.json file is inside my project name folder.
Also my dependencies from Firebase are the following:
"dependencies": {
"firebase": "^6.5.0",
"firebase-admin": "8.4.0",
"firebase-functions": "^3.2.0"
},
My directory structure is the following:
-server
-idea
-bin
-functions
-node_modules
-public
-routes
-views
-.firebaserc
-.gitignore
- app.js
- firebase.json
- master
- package.json
- package-lock.json
- serviceAccountKey.json ===> ***
- server.iml
Why am I getting this error?
Note: I've done some research on this and my file path does conform with the node.js documentation on using the require method.
Put your serverAccountKey.json file in your "functions" folder. The entire contents of that gets packaged up and sent to Cloud Functions (unless you changed that default in firebase.json). Files that are outside that folder will not be available. Right now, it looks like the file is in "server".

How do I put Firebase-Admin credentials in an Environment Variable?

I'm trying to setup a Firebase project on Codeship CI/CD and I cannot seem to get the service credentials JSON key to work with my project when it's stored in an environment variable.
MVCE
Dev environment startup script
export FIREBASE_CREDENTIAL=BASE64ENCODED_credentials_json
export DATABASE_URL=https://my-project.firebaseio.com
echo $FIREBASE_CREDENTIAL | base64 -D > FIREBASE_CREDENTIAL.json
npm start
index.ts
import * as admin from "firebase-admin";
const credentials = require("../FIREBASE_CREDENTIAL.json");
const databaseURL = process.env.DATABASE_URL;
console.log(credentials.project_id); // succeeds
console.log(databaseURL); // is correct
admin.initializeApp({
...credentials,
databaseURL,
databaseAuthVariableOverride: { uid: "scraper" }
});
admin
.database()
.ref("/test")
.set(admin.database.ServerValue.TIMESTAMP);
Codeship script
echo $FIREBASE_CREDENTIAL | base64 -di > FIREBASE_CREDENTIAL.json
nvm install 8
npm install
npm test
Results
On dev machine, the write succeeds as expected
On Codeship CI container, I get this error.
console.warn node_modules/#firebase/logger/dist/index.cjs.js:66
[2018-10-05T12:53:51.316Z] #firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"Failed to parse access token response: Error: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND\"."}

Using Firestore and algolia error projectId must be a string in FirebaseApp.options

I've been following a tutorial here https://medium.com/#soares.rfarias/how-to-index-firestore-data-into-algolia-using-cloud-functions-9ed5b56baa57 on how to link google firestore with algolia search service and although I've got through most the steps, (I was a little stuck at running the indexing script but im now past that) I'm stuck on migrating all collections from A to B I get an error that means almost nothing to me as I'm unaware of any firestore project id and i definitely dont interact directly with the folder the error points at, here is the entire error
C:\Users\marti\Desktop\sealstudios\functions\node_modules\#firebase\firestore\dist\cjs\src\api\database.js:211
throw new error_1.FirestoreError(error_1.Code.INVALID_ARGUMENT, 'projectId must be a string in FirebaseApp.options');
^
FirebaseError: projectId must be a string in FirebaseApp.options
at new FirestoreError (C:\Users\marti\Desktop\sealstudios\functions\node_modules\#firebase\firestore\dist\cjs\src\util\error.js:142:28)
at Function.Firestore.databaseIdFromApp (C:\Users\marti\Desktop\sealstudios\functions\node_modules\#firebase\firestore\dist\cjs\src\api\database.js:211:19)
at new Firestore (C:\Users\marti\Desktop\sealstudios\functions\node_modules\#firebase\firestore\dist\cjs\src\api\database.js:110:43)
at Object.firestore (C:\Users\marti\Desktop\sealstudios\functions\node_modules\#firebase\firestore\dist\cjs\src\platform\config.js:44:76)
at FirebaseAppImpl._getService (C:\Users\marti\Desktop\sealstudios\functions\node_modules\#firebase\app\dist\cjs\src\firebaseApp.js:117:66)
at FirebaseAppImpl.(anonymous function) [as firestore] (C:\Users\marti\Desktop\sealstudios\functions\node_modules\#firebase\app\dist\cjs\src\firebaseApp.js:304:31)
at Object.serviceNamespace [as firestore] (C:\Users\marti\Desktop\sealstudios\functions\node_modules\#firebase\app\dist\cjs\src\firebaseApp.js:289:32)
at Object.<anonymous> (C:\Users\marti\Desktop\sealstudios\functions\indexing.js:12:21)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
any help appreciated
The problem may be in the firebase settings. After deploing my hosting application, I deleted the settings in config / firebase.js and then found the same error on the local 8080 server.
My settings now:
export default {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
}
Check your connection settings =)
I found the same problem, When, I deploy my code on Heroku. But, that is resolved by adding environment variables on Heroku. Because, All the details for the place holders should be strings except the database URL.
So, If you have setup your variable on local in .env, you have to add the environment variables on that server where are you deploying.

Resources