Access to Firebase Storage with Firebase Authentication
I have to admit, I was excited and scared at the same time, when I got tasked with building a web app based on Google Firebase
All the API endpoints and logic was provided by the folks who programmed the Android app (disclosure of the shop, when the project is finished). They did a great job designing the API!
The web backend is used to add new users and create reports from checklists.
Working with JSON endpoints is no problem. Confusion arose when I had to deal with Firebase Authentication and lately with Firebase Storage.
There are some libraries for Python and also PHP. Unfortunately, when I started the project the kreait/firebase-php: Firebase Admin PHP SDK wasn't complete yet. So I was rolling out my own wrapper und tried to make sense ouf Firebase Authentication and their REST API. It took some time to understand, that the Firebase Auth Service just works with the Google Identity Platform. Who knew... And yes, they recommend to move to the Firebase JS SDK, but under the hood they still call
With having solved the identity crisis and being able to obtain and refresh a valid token to sign all my API calls, the next big step was to access Firebase Storage.
It took me two days to figure out what was going on, trying to make sense of the documentation and one year old stack overflow solutions that never fit my use-case.
One of the API endpoints returns Firebase Storage objects like this:
The first thing I did was grabbing the value of
fileReferences.0.mediaLink and send a request with my Firebase Auth token in the header. This ended in "Permission denied" notices. Even after the devs made the Firebase Storage publicly available.
You can use the Google Cloud Storage APIs to access files uploaded via Firebase SDKs for Cloud Storage, especially to perform more complex operations, such as copying or moving a file, or listing all the files available at a reference.
It's important to note that these requests use Google Cloud Storage ACLs or Project Level IAM, rather than Firebase Authentication and Storage Security Rules.
WHAT?!?! "Project Level IAM, rather than Firebase Authentication and Storage Security Rules"? What does that even mean?
The documentation of the underlying Google Cloud Storage REST API didn't help much. It showed it must be possible, but it rejected my Firbase Auth and instead talked about Google Serive Accounts etc. I almost gave up.
The SDK's as easy as they are, they are black boxes. If you don't reverse engineer them, you don't know what happens inside, when you call something like
The SDK builds an url after this scheme, to obtain media:
To be precise, this is how to create the final media url:
storageBucket is the value you get when you setup Firebase.
A call (signed with my Firebase Auth Bearer Token!) to
https://firebasestorage.googleapis.com/v0/b/bucket.appspot.com/o/projects%2F-s0m31D%2Fpicture.jpg returns the following meta data:
"contentDisposition": "inline; filename*=utf-8''picture.jpg",
EUREKA there it is, the
Tests in Postman
Now there are two ways to download the image (or to display it in the browser):
1. Authentificate the call with the Firebase Auth Bearer Token
curl -X GET \
-H "Authorization: Bearer [TOKEN]" \
-o "picture.jpg" \
2. Share publicly with the
curl -X GET \
-o "picture.jpg" \
token=xyz URL parameter, the image is now available to everyone who has access to the complete link.
This is how to download objects from the Firebase Storage using your Firebase Authentication.
If you think I'm wrong or you want to add some information, please do at this Stack Overflow question