OAUTH token for firebase cloud storage? - firebase

I am trying to upload an image to my cloud storage on firebase on an application where I won't have access to googles client libraries. From my understanding, I've followed this documentation on uploading images:
curl -X POST --data-binary #OBJECT_LOCATION
-H "Authorization: Bearer OAUTH2_TOKEN"
-H "Content-Type: OBJECT_CONTENT_TYPE"
"https://storage.googleapis.com/upload/storage/v1/b/BUCKET_NAME/o?uploadType=media&name=OBJECT_NAME"
Specifically, on postman I added the file location, the OAUTH_TOKEN I believe I used was from this location:
enter image description here
However, I've run into an unauthorization error, but I did change my rules on firebase cloud to allow anyone to read/write just for testing. However just to clarify, I can use the bucketname on my cloud storage as well correct? I guess the problem must lie in not having the right OAUTH token which I am still confused on how to generate. any help is greatly appreciated.

Related

Upload images to firebase storage with rest api

Is it possible to upload an image to firebase storage with the rest api?
I heard that firebase storage have no rest api but google cloud storage have one. Hoe can I achieve this to upload a image in firebase storage using the google cloud storage rest api?
What you've heard is correct: there is no specific REST API for uploading file to Cloud Storage Firebase. The only option to upload through Firebase is to use one of the SDKs. And the only option for uploading through a REST API is to use the REST API for Cloud Storage.
From the documentation on uploading a file through the REST API comes this example using CURL:
curl -X POST --data-binary #[OBJECT_LOCATION] \
-H "Authorization: Bearer [OAUTH2_TOKEN]" \
-H "Content-Type: [OBJECT_CONTENT_TYPE]" \
"https://storage.googleapis.com/upload/storage/v1/b/[BUCKET_NAME]/o?uploadType=media&name=[OBJECT_NAME]"
A few things to keep in mind when using this REST API to uploading data to Cloud Storage:
You're uploading directly to Cloud Storage, so need to authorize with an OAuth access token, instead of Firebase Authentication.
Uploads through this API bypass the Firebase security rules you've set up for Storage, so you'll want to either do this from a server only, or control access to the bucket at the Google Cloud Storage level.
Firebase will not generate a download URL for uploads through this REST API automatically. If you want a download URL for a file, you can do so from one of the Firebase SDKs after uploading the file.

How can i secure my Firebase "subscribe to topic" from the web client?

In firebase FCM i can subscribe to a topic through the web client like this:
curl -X POST -H "Authorization: key=<your_server_key>" -H "Content-Type: application/json" -H "Content-Length: 0" "https://iid.googleapis.com/iid/v1/<your_instance_token>/rel/topics/<topic_name>"
The only usage i have form Firebase is sending push notifications form the server to web clients that are subscribed to a specific topic.
But how can i prevent unwanted people from subscribing to my topics?
FCM topics are not "secure". Anyone can subscribe to them. You should not distribute sensitive data over a topic.
If you want to send sensitive data to only certain users, you should be using device tokens to send messages directly. Either that, or accept the fact that any client can "listen in" on the topic, if they just know the name of the topic and the configuration of your project (which is essentially public since it gets baked in your app at build time).

Firebase: 403 PERMISSION_DENIED (FirebaseError: Installations): Requests are blocked, after updating SDKs (FirebaseInstallationsService)

I updated the Firebase SDKs of my Firebase for Web application.
Since the update my application no longer starts and throws the following error:
Any idea what is going on?
Uncaught (in promise)
FirebaseError: Installations: Create Installation request failed with error "403 PERMISSION_DENIED: Requests to this API firebaseinstallations.googleapis.com method google.firebase.installations.v1.FirebaseInstallationsService.CreateInstallation are blocked." (installations/request-failed).
It turns out that new versions of Firebase SDKs depend on a new internal infrastructure service, called FIS (the Firebase Installations Service) for targeting identifiers ("FIDs" or "Instance-IDs").
If you are using API key restrictions for the API keys you use in your application, you will have to extend those restrictions to allow usage with the new Firebase Installations Service at firebaseinstallations.googleapis.com.
To allow your API key in question to be used with the new Firebase Installations API:
go to the Google Cloud Console
choose the relevant project (i.e. the project you use for your application)
open the menu and go to APIs & Services -> Credentials
click Edit API key for the API key in question
scroll down to API restrictions
from the dropdown, choose Firebase Installations API
click Save
wait a couple of minutes for Google servers to update and retry...
Note: If you cannot find the Firebase Installations API in the list of APIs, you might first have to enable the API for your project (to do so click here).
Note: If you are not sure which API key is used in your application, you can check the usage numbers of Firebase Installations API per API key.
Note: Verify your fix by checking if you can see successful 200 requests increasing on the Firebase Installations API request metrics page.
Test if your configuration works with the following CURL command:
api_key=<YOUR_API_KEY>;
project_identifier=<YOUR_PROJECT_ID>;
app_id=<YOUR_FIREBASE_APP_ID_SIMILAR_TO_1:12345678:android:00000aaaaaaaa>;
curl -H "content-type: application/json" -d "{appId: '$app_id', sdkVersion: 't:1'}" https://firebaseinstallations.googleapis.com/v1/projects/$project_identifier/installations/?key=$api_key;
If your API key uses App restrictions you will have to expand your CURL request with the respective HTTP headers identifying your application:
Android: -H "x-android-package: com.rayo.example.app" -H "x-android-cert: 1234567890ABCDEF1234567890ABCDEFAABBCCDD"
iOS: -H "x-ios-bundle-identifier: com.rayo.example.app"
Webapp: -H "Referer: https://www.your.webapp.com/page?p=1"

How can I get a token?

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

Messaging using Firebase do not behave like Firebase console

I can succesfully send a post request from my server at home to the Firebase server. The message is delivered to the client without a problem.
Except that the app must be either running or in the background.
If the app is closed the message goes to limbo. There is nothing in the system tray, there is no sound, nothing.
Is this the expected behavior?
This is the curl command I am using:
curl -X POST --header "Authorization: key=AIzaSyBntseBqux9nBX8y" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"fxhL9uQBYUo:APA91bGaEWqTJ2mde1vSlfOZWHYrYTQLbZdFih2hs_wszaxPsthfMN5lnxH6RHquUImmT\",\"data\":{\"action\":\"run\"}}"
Am I missing some extra fields that make the device act like it is doing now?
PS: Server Key and Token are not real.
Your code is sending a data message, the Firebase Notifications console sends notifications messages.
One big difference between these two types of messages is that notification messages are automatically shown in the system notification area if your app is not in the foreground.
See this page in the Firebase documentation explaining the difference between these message types.

Resources