How can I get a token? - firebase

I need to get an access-token so that I can have access to my Firebase Cloud Storage (I specify this in the query:
"Authorization", "Bearer + token").
For this, I have already downloaded Cloud SDK, created a JSON file from Google Console and registered the command:
$gcloud auth activate-service-account --key-file=C:\Project123.json
Can you please tell me how to get the access-token from this to paste it in Authorization?

*Answer by OP:
(posted as comment by OP)
I was able to find information in Google. For anyone who encounters a similar problem:
You want to extract access-token from any JSON file for any access, then DO NOT need to make login using application-default login, instead you just need to use:
set GOOGLE_APPLICATION_CREDENTIALS =/path/to/service_account_file.json
Then just run:
gcloud auth application-default print-access-token
I hope someone helped. Good luck

Related

Firebase storage returning 412 error even after enabling storage API

We have already used firebase storage for storing the images and files in our application. Recently our files seem to bee not loading in the application and once cross-checking I found the following error.
{
"error": {
"code": 412,
"message": "A required service account is missing necessary permissions. Please resolve by visiting the Storage page of the Firebase Console and re-linking your Firebase bucket or see this FAQ for more info: https://firebase.google.com/support/faq#storage-accounts"
}
}
After further investigation, I found a solution in the below-related FAQ.
https://firebase.google.com/support/faq/index#storage-accounts
As per the documentation, I have created a new service account with the I am admin role as Cloud Storage for Firebase Service Agent. It created a service account called service account name service-PROJECT_NUMBER#PROJECT_id.iam.gserviceaccount.com. Also, I have enabled the IAM admin policies for managing services. But still but storage URL is not loading.
Do I need to add any more changes to storage permissions? How can I use the existing storage bucket data with the new updates?
I just fixed this for my self.
Create a bucket in Cloud Storage
Get your projectname and bucket name
Go here: https://firebase.google.com/docs/reference/rest/storage/rest/v1beta/projects.buckets/addFirebase
Press Try IT
Fill in your projectname en bucket name. Press Execute é voila!
See my screenshot here
As mentioned in this github.
On the IAM page with the project owner role you will have an option to Grant Access.
add this service account.
service-project number#gcp-sa-firebasestorage.iam.gserviceaccount.com as a principle, Cloud Storage for Firebase Service Agent as role.
If you are having trouble adding the service account (service-#gcp-sa-firebasestorage.iam.gserviceaccount.com) to the project, it's possible that Cloud Storage for Firebase has not created it yet.
Try using the AddFirebase API to relink a bucket to your project and try again: You can refer to this
I fixed the issue with the following steps.
After setting the IAM policies in storage admin I tried with AddFirebaseAPI function using postman by passing the project id and storage bucket name. But it returns a not found error for me. I tried it with both project number as well as id. Both given the same error.
About the error using Postman and the URL this should be working it solved by using
https://firebasestorage.googleapis.com/v1beta/projects/<projectid>/buckets/<bucketname>:addFirebase
Here is an example of the HTTP call that the playground gives you
POST
https://firebasestorage.googleapis.com/v1beta/projects/<projectid>/buckets/<bucketname>:addFirebase?key=[YOUR_API_KEY] HTTP/1.1
Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
Content-Type: application/json
It will returns your bucket instance. Also it may takes a few mins to reflect the changes.
If you open firebase-storage in console.firebase.google.com, it gives you an alert saying attach permission. If you click on it, problem should be fixed automatically.
If it doesn't solve, then follow this Github
If you don't find project number #gcp-sa-firebasestorage.iam.gserviceaccount.com in console permission, then make sure you click on attach permission dialog in firebase storage.
This is how my problem was solved.

Error: Failed to get Firebase project project-name. Please make sure the project exists and your account has permission to access it

I tried running firebase deploy like I have always done but came across this Error:
Error: Failed to get Firebase project project-name. Please make sure the project exists and your account has permission to access it.
I have run firebase login and I get 'Already logged in as email#example.com'
The firebase-debug.log file shows a few 401 http responses and some of them have the following text:
'Request had invalid authentication credentials' and also 'invalid token'
How can this be solved?
For me this worked,
firebase logout
firebase login
This worked for me:
Run
firebase use --add
And re-select the project
Simply
use firebase login --reauth
For me, I was setting up a new machine with an existing firebase project. This is what I did:
First successfully login to firebase from your command console
firebase login
Get a list of your projects
firebase projects:list
Tell firebase which project you are using
firebase use <Project ID>
Then, I was able to deploy and use other commands for this project. Hope this helps someone.
sometimes there is a token error you can check debug.log
Error Message in debug.log : [debug] [2020-10-13T23:01:28.050Z] <<< HTTP RESPONSE BODY {"error":{"code":401,"message":"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","status":"UNAUTHENTICATED"}}
Solution of error use
firebase login --reauth
and again firebase init for reinitializing the project
select project and
firebase deploy
Run
firebase use --add
And re-select the project
This works 100%
Try logging out of firebase CLI and then log back in with the account that has the project that you are trying to run.
This was the case with me. I was logged in to another firebase account.
Steps:
`firebase logout`
`firebase login`
Any typo inside the .firebaserc would show this error. For me I had an extra comma, removing it solved the error.
Disconnecting the company's VPN and trying it again worked for me.
If none of the other answers worked for you, I had the same problem and received this solution from Firebase Support. (ty Sergei)
My system will occasionally create and Environment Variable named FIREBASE_TOKEN. The token will expire, but the variable does not. Restarts and logout/login cycles did nothing. Deleting the variable works every time though.
Remove-Item Env:\FIREBASE_TOKEN
It's really awesome and confusing------>
just did these command ->
> firebase logout
>
> firebase login
if you use Mac or Linux, you can write this command export http_proxy=http://127.0.0.1:1087 to make sure your terminal uses the proxy, which is useful for me.
None of the above solutions worked for me but removing/editing the stored value of the default project in $HOME/.config/configstore/firebase-tools.json did work.
If you have this issue in a CI pipeline, you can regenerate a token on your local machine with
firebase login:ci
Put the generated token as an environment variable, e.g. FIREBASE_TOKEN and run like this:
firebase deploy --token "$FIREBASE_TOKEN"
1.run this
firebase logout
firebase login
2.if u can not login successfully , like this ?
[https://i.stack.imgur.com/4MJM5.png][1]
run this , export http_proxy=http://localhost:your proxy port
4.successfully!!!
[https://i.stack.imgur.com/sRcvG.png][1]
It is possible you are trying to deploy without internet connection. The first thing to look for is if your internet is okay.
Make sure that you don't have this file .firebaserc already existing in your project directory if you already do!
Delete it and try firebase init in console again.
I got this error in Linux when I was out of disk space, freeing some space resolved it.

How to upload the files to firebase storage with uid using Post Url?

I found one solution to upload the file to firebase storage without any authentication using this link How to upload objects to Firebase Storage using Postman for testing?
The above-mentioned case works only when my firebase storage looks like this, (Without any security restriction)
allow read, write;
But, Now I want to achieve this with some security restrictions.
Is there any way to upload the files to firebase storage by POST URL (Postman) with some security restriction.
I tried to achieve this by
https://firebasestorage.googleapis.com/v0/b/projectName.bucketName.com/o?uploadType=media&name=picture2&auth=uid
But it shows 403 - forbidden error.
There is no public REST API for uploading file to Cloud Storage for Firebase. The end point you're trying to reach is meant for use by the Firebase SDK only, and is neither documented, nor supported for use beyond that.
That said, you may be able to mint a token using the Firebase Authentication REST API, and pass that along to the request you have. But as said, it won't be supported and may change without warning.
The most common approach for REST uploads is through the Google Cloud Storage API, around which the Firebase APIs are a friendly wrapper. But these APIs are meant for access from trusted code, so wouldn't be using the Firebase Authentication UID of your users. The best I can think of is to write a Cloud Function that handles the user authentication and authorization, and then use the Google Cloud Storage Node.js or REST API to upload the file.
I use this endpoint on my project
https://firebasestorage.googleapis.com/v0/b/YOUR_BUCKET/o?uploadType=media&name=YOUR_FILE_PATH_AND_NAME
and add headers on your post
headers.Add("Authorization", "Bearer " + FirebaseAuthIDToken);
headers.Add("Content-Type", "application/octet-stream");

Firebase auth password reset sends expired link

I'm creating a flutter app and use Firebase Auth, everything works just fine including Facebook and Google sign ins, however, once password reset link is sent via email, it shows up as already expired.
I have found this answer which states "you need to include the app itself which is where the emails originate: .firebaseapp.com", I don't understand what type of credential needs to be added. Could someone look at the images below and advice what is missing? Or is this not the problem in my case? What else should I check?
Credentials:
Firebase console:
Webpage:
In case anyone runs into an issue like this, here's what solved my issue (thank you, Firebase support!!!).
Go to Cloud Console, under APIs & Services > Credentials
Select API key that has "Restrictions" as "HTTP referers" (See question picture "Credentials").
Under "Application restrictions", add https://Project_ID.firebaseapp.com (hence you need to copy your Project ID from the firebase console (See question picture "Firebase console")
In my case I had AWS cognito credentials there and since I've moved to Firebase it was not updated.

How to retrieve list of Firebase authenticated users and download them in JSON format?

I am new to firebase, I wanted to retrieve list of my authenticated users in JSON Format
I have searched for the solution but couldn't have found, some say it isn't possible !!!
Please, someone, tell me whether it is possible or any other alternative ways to
get my users username/displayName/emailId or any other description based on their SignUp method.
I found a way to do it but I got an error , using Nodejs with this command :
**firebase auth:export users.json --format=json --project projectname**
This is a rather indirect answer, but, when they sign up, you could add the info you need about the user to the Firebase live database. Then, when you retrieve that information from the database, it's in JSON. I guess this probably isn't the answer you were looking for, but I hope it helps.
Ensure that you have firebase-cli installed on your computer. If so, just use firebase auth:export <name.format> --project <project-id> from the command line.
You can read more about the exported file from their tutorials here.

Resources