I’m currently developing an app using Firebase Realtime database, however I’ve opted to omit using the SDK to retrieve data from the db. The reason being I don’t want my code to be so closely tied with FireBase as once the app is built the api itself will be moving to a custom rest based api.
I’ve implemented the api calls using REST with Firebase as per the docs without issue – POST, GET, DELETE etc..
The issue I have is if I enable any kind of authentication on the database, according to the docs I need to send “access_token” with the request however I don’t know where to retrieve this from. firebaseUser.getToken(true)
returns what looks to be a JWT token that isn’t recognised if I send it as "access_token"
. I get 401 Unauthorized
I also followed the instructions to setup a service account which seems to generate a token that works but then it doesn’t uniquely identify the user.
So my question is can anyone point me in the direction of how to get the required access token that identifies which user is accessing that api? The login options my Firebase project supports are Google, Facebook & Twitter.
2
Answers
If you are looking for the different tokens or IDs from each one of the different authentication modes, you should implement differences APIs for each one of them:
REST API
A successful request will be indicated by a
200 OK
HTTP status code. The response contains the data being retrieved:The Database REST API accepts
access_token=<TOKEN>
on the query string or headerAuthorization: Bearer <TOKEN>
to authenticate a request with a service account.The following example demonstrates how you might use this with a database containing user names. You would replace
[PROJECT_ID]
with the identifier of your Firebase project.Facebook
You should add the
Facebook SDK
to your application and implement aLoginButton
andLoginManager
items asking for some information as public_profile an email. It’s pretty annoying to work withFacebook SDK
. An Example code about how to add it is this:Also, inside the developers console from Facebook you should create an account, configure a new project with your app package name and add the SHA keys for debug and release versions of your app. After do all this things, you will successfully retrieve a token from the
LoginResult
object using the methodgetAccessToken()
You can read more about this in the official documentation.
Google
Google is easier because it is already connected to Firebase, you should add to your Gradle
google play services
and add agoogle services JSON
file already configured to your application. You can get it from your Firebase console.After this, you will need to configure a
GoogleSignInOptions
item using the id from your JSON file:After this you will just need to make a intent to the
GoogleSignInApi
in your app and wait for theonActivityResult
callback:After this you will be able to retrieve the token from the GoogleSignInAccount item. Remember to configure different SHA keys for debug or release versions of your app or Google Sign in will stop working in release version.
You can read more about this in the official Firebase documentation
Twitter
About Twitter, I didn’t work with Twitter, so I can’t really help you at the moment, but I suggest you to check the Twitter developer documentation and the firebase Twitter implementation post.
I will try to edit this when I will make some pocs at home checking how it works.
About Firebase tokens
Another good point to have knowledge about is the Firebase id tokens, which are unique per user and connection in your app, allowing you to check if the same account is trying to log from different devices at the same time, or send FCM Cloud Messages to use online notification in your app.
To retrieve it, you should use the
FirebaseInstanceId
object using the API and the methodFirebaseInstanceId.getInstance()
. This will retrieve you a FirebaseInstance unique ID for your user when he/she login in your app.You can retrieve his token with the
idInstance.getToken()
and store it whenever you want in your application to check it and manage it in the way that you want.The Firebase documentation about this is not pretty clear, so I recommend you to use the next link, it helped me a lot to implement it in my app.
You can add a table in your server for USER.
In that table, add fields like firebase_token, google_token, fb_token, etc.
As you register in all those services one by one, update the fields in the table for user.
In this way you can maintain the users_info as well as all the required tokens.