Using firebase RestApi to signup users using email and password

Viewed 147

I'm creating a project where users need to able to log in to their account and see some data. I'm creating a windows application with unity, so from what I understood from researching, I have to use the firebase RestAPI, not the SDK. I managed to use the realTime database but I'm struggling with the authentication side of things.

I followed this tutorial and used the documentation for signing up users into firebase with the RestAPI. I keep getting a 400 (Bad Request) error. I found this post, where the solution was to use a strong password, but that didn't work.

Since I'm using a not so reliable unity c# package as a client, I tested my code with nodeJs as well. Same error.

My code:

C#

private void SignUpUser(string email, string username, string password)
{
    string userData = "{\"email\":\"" + email + "\",\"password\":\"" + password + "\",\"returnSecureToken\":true}";     
    // Content type is json by default   
    RestClient.Post<SignResponse>("https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=" + AuthKey, userData).Then(
        response =>
        {
            Debug.Log("Success");               
        }).Catch(error =>
    {
        Debug.Log(error);
    });
}

Javascript

const axios = require("axios");

axios
    .post(
        'https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=' + API_KEY,
        {
            email: "myEmddail@example",
            password: "superStrongzi344##",
            returnSecureToken: true,
        }, 
        {
            'Content-Type': 'application/json',            
        }
    )
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });

Part of response:

response:
   { status: 400,
     statusText: 'Bad Request',
     headers:
      { expires: 'Mon, 01 Jan 1990 00:00:00 GMT',
        pragma: 'no-cache',
        date: 'Sun, 10 May 2020 21:09:52 GMT',
        'cache-control': 'no-cache, no-store, max-age=0, must-revalidate',
        vary: 'X-Origin, Referer, Origin,Accept-Encoding',
        'content-type': 'application/json; charset=UTF-8',
        server: 'ESF',
        'x-xss-protection': '0',
        'x-frame-options': 'SAMEORIGIN',
        'x-content-type-options': 'nosniff',
        'alt-svc':
         'h3-27=":443"; ma=2592000,h3-25=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',        'accept-ranges': 'none',
        connection: 'close',
        'transfer-encoding': 'chunked' },
     config:
      { url:
         'https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=API_KEY',
        method: 'post',
        data:
         '{"email":"myEmddail@example","password":"superStrongzi344##","returnSecureToken":true}',

Is there anything I'm missing?

Thanks

1 Answers

Try enabling registering with e-mail in your firebase console. Also the c# library you're using doesn't seem very reliable and might not be well suited for error handling, I would suggest the native System.net.http library that's built in. An example of a request:

using System.Net.Http;
private static readonly HttpClient client = new HttpClient();

var values = new Dictionary<string, string>
{
    { "thing1", "hello" },
    { "thing2", "world" }
};

var content = new FormUrlEncodedContent(values);

var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);

var responseString = await response.Content.ReadAsStringAsync();
Related