+2 votes

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! :(

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! :D

in Engine by (14 points)

1 Answer

+2 votes

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 :
https://github.com/WolfgangSenff/GodotFirebase/
it is a great plugin, you need to update the links to the new links and everything should work fine.

by (26 points)

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

You are welcome, glad it worked :).

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?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.