Create user accounts using Google's Firebase REST API

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Hanna

I’m trying to use Google’s Firebase REST API to create new accounts in my android application (following Guilherme’s tutorial https://www.youtube.com/watch?v=Udm7uDQM05w). However I cannot get is to work! :frowning:

Whenever the button for creating a new account is pressed there is a function call to register(email: String, password: String, http: HTTPRequest)

const API_KEY := [API KEY]
    
const REGISTER_URL := "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=%s" % API_KEY
    
func register(email: String, password: String, http: HTTPRequest):
    var body := {
        "email": email,
        "password": password,
        "returnSecureToken": true,
    }
    var error = http.request(REGISTER_URL, _get_request_headers(), 
    						false, HTTPClient.METHOD_POST,
    						to_json(body))
    var result := yield(http, "request_completed") as Array
    if result[1] == 200:
    	# Save current token

When the http request is completed _on_HTTPRequest_request_completed(result, response_code, headers, body) is called.

func _on_HTTPRequest_request_completed(result, response_code, headers, body):
    var response_body := JSON.parse(body.get_string_from_ascii())
    if response_code != 200:
        notification.text = response_body.result.error.message.capitalize()
    else:
        notification.text = "Welcome!"
        yield(get_tree().create_timer(2.0), "timeout")
        get_tree().change_scene("res://Scenes/Login/Login.tscn")

The body of the http request is empty and I get and error trying to parse it: JSON.parse(body.get_string_from_ascii()).

I have tried to use Postman to make requests to their endpoints and it works fine. It also works fine to make requests from Postman to Firebase.

Is there any changes in Godot 3.2 that I need to know about in order to get this to work? Any clues on how to solve this would be greatly appreciated! :smiley:

:bust_in_silhouette: Reply From: kamel

Hello,
it is recommended to use the links and information in Firebase auth documentation, here is the link :
https://firebase.google.com/docs/reference/rest/auth
as you can see in the description you have to specify content-type header to be application/JSON.
am currently building a game that uses firebase, am using this plugin :

it is a great plugin, you need to update the links to the new links and everything should work fine.

Thank you for the clue! I wasn’t aware of the content-type header. Just including it to my request made it work. :slight_smile:

Hanna | 2020-04-30 11:38

You are welcome, glad it worked :).

kamel | 2020-04-30 15:41

Hello brother, thanks to your comment I found this plugin that works.
But I can’t find a tutorial on how to insert data. Just how to log in and nothing else.
Could you give me a hand?

SrBoga | 2021-04-18 05:04