Reputation: 415
I am developing an Android app which uses Firebase and my own server running Django. What I intend to do is, I want to first authenticate the user using android app to the django server which then generates the custom tokens as specified in firebase docs. Then I want to send the generated custom token back to the android.
My question is how to send that custom token back to android? I tried to send as JSON object. But it says JWT are not JSON serializable.
I passed the username and password from android app as json object and authenticated with my django server.
Here is my minimal Django
code:
import firebase_admin
from firebase_admin import credentials
from firebase_admin import auth
cred = credentials.Certificate("firebase-admin.json")
default_app = firebase_admin.initialize_app(cred)
def validateuser(request):
json_data=json.loads(request.body.decode('utf-8'))
try:
// I verify the username and password and extract the uid
uid = 'some-uid'
custom_token = auth.create_custom_token(uid)
result={'TAG_SUCCESS': 1, 'CUSTOM_TOKEN': custom_token }
except:
result={'TAG_SUCCESS': 0, 'CUSTOM_TOKEN': '0'}
return HttpResponse(json.dumps(result), content_type='application/json')
But it says the custom token is not JSON serializable. Is it not the way to do like this? How do I send the custom token back to the android app?
And this is the error:
uid: 78b30d23-6238-4634-b2e4-73cc1f0f7486
custom_token: b'eyJraWQiOiAiZmFlNzA2MzZiY2UwZTk0Y2Y5YTM2OWRlNzc4ZDZlYWQ5NGMwM2MzYiIsICJhbGciOiAiUlMyNTYiLCAidHlwIjogIkpXVCJ9.eyJpc3MiOiAiZmlyZWJhc2UtYWRtaW5zZGstOXBtbjVAYnVzdHJhY2tlci0xZDE3OS5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsICJ1aWQiOiAiNzhiMzBkMjMtNjIzOC00NjM0LWIyZTQtNzNjYzFmMGY3NDg2IiwgImF1ZCI6ICJodHRwczovL2lkZW50aXR5dG9vbGtpdC5nb29nbGVhcGlzLmNvbS9nb29nbGUuaWRlbnRpdHkuaWRlbnRpdHl0b29sa2l0LnYxLklkZW50aXR5VG9vbGtpdCIsICJleHAiOiAxNTA4MDc2OTA4LCAiaWF0IjogMTUwODA3MzMwOCwgInN1YiI6ICJmaXJlYmFzZS1hZG1pbnNkay05cG1uNUBidXN0cmFja2VyLTFkMTc5LmlhbS5nc2VydmljZWFjY291bnQuY29tIn0=.jgexW_xR5FeZvuO5TPWO8EOBnRJ28ut9OR_OxeajE1_o4ns4fwd2pMXlK2GkM464P5Vi-IxheG-IIJcANxGSDeZgvgpkLfKkHMZeSaraqfEQGq6N7ipuD8o1T7zd5qm79twmFbrQZRB1y7g1-zcjL69x8KFsThWOTmo0TYj5l3zf8_2Cxbw2SGefMWkCwL0d1yQjcUqVyuSAP3-Sg8KrrqCcG4cjNOXKeWxwbUQO7DobOQlT5TfRApwWk8Td6uPjD7d6jqMo-HPKOis0vRoXMBzflZKj36-hIOFkygZNbDWLTsQzbb3HZg8dBabA5GTy--iQi038TRMIm2W0irr0ng=='
Internal Server Error: /api/user/validateuser/ Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner response = get_response(request) File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 249, in _legacy_get_response response = self._get_response(request) File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) File "/home/ubuntu/www/Tracker/user/api/views.py", line 251, in validateuser return HttpResponse(json.dumps(result), content_type='application/json') File "/usr/lib/python3.5/json/init.py", line 230, in dumps return _default_encoder.encode(obj) File "/usr/lib/python3.5/json/encoder.py", line 198, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode return _iterencode(o, 0) File "/usr/lib/python3.5/json/encoder.py", line 179, in default
raise TypeError(repr(o) + " is not JSON serializable")
Upvotes: 1
Views: 803
Reputation: 356
I think you are using python3 version. I found what the problem is.
The auth.create_custom_token(uid) method returns a bytes literal and bytes literal are not JSON serializable. That is why you are getting the error. You can see b' infront of the jwt token.
So, you need to just convert the byte literal to string using the code below.
custom_token = (auth.create_custom_token(uid)).decode()
Upvotes: 6