1

I was trying to use the rest api patch request for identity toolkit https://identitytoolkit.googleapis.com/v2/projects/{projectId}/config?key={apiKey} I replaced the project id with what I have on my console and the apikey with the firebase web api key but it is not working. Any idea on how I use this request to enable email/password firebase authentication?

   var credential = GoogleCredential.GetApplicationDefault()
        .CreateScoped(iam.IamService.Scope.CloudPlatform);

    var service = new iam.IamService(new BaseClientService.Initializer
    {
        HttpClientInitializer = credential
    });

    var projectId = "myprojectidhere";
    var serviceAccountName = "auto-service-acc";
    var displayName = "Auto Service Account";

    var serviceAccount = new iamData.ServiceAccount
    {
        DisplayName = displayName
    };

    var createRequest = new iamData.CreateServiceAccountRequest
    {
        AccountId = serviceAccountName,
        ServiceAccount = serviceAccount,
        // Optional: specify a list of roles for the new service account.
        //Roles = new List<string> { "roles/editor" }
    };

    var createdAccount = service.Projects.ServiceAccounts.Create(
        createRequest, $"projects/{projectId}").Execute();

    var serviceAccountEmail = createdAccount.Email;

    var accessToken = credential.UnderlyingCredential.GetAccessTokenForRequestAsync(
        iam.IamService.Scope.CloudPlatform).Result;




    string apiKey = "myapikeyhere";

    var url = $"https://identitytoolkit.googleapis.com/v2/projects/{projectId}/config?key={apiKey}";
    var requestBody = new 
    {
        // projectId,
        signIn = new
        {
            email = new
            {
                enabled = true,
                passwordRequired = true
            }
        }
    };

    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

    var response = await httpClient.PatchAsync(url, new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json"));
    Console.WriteLine(response);
    if (response.IsSuccessStatusCode)
    {
        // Authentication using email and password is now enabled.
        Console.WriteLine(" yes it is enabled");
    }
    else
    {
        // Handle the error.
        // Console.WriteLine(" no it doesn't work");
        var errorMessage = await response.Content.ReadAsStringAsync();
        Console.WriteLine($"Error: {errorMessage}");
    }

Error:

StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Date: Tue, 28 Mar 2023 08:29:15 GMT
  Pragma: no-cache
  Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  Vary: X-Origin
  Vary: Referer
  Vary: Origin,Accept-Encoding
  Server: ESF
  X-XSS-Protection: 0
  X-Frame-Options: SAMEORIGIN
  X-Content-Type-Options: nosniff
  Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
  Accept-Ranges: none
  Transfer-Encoding: chunked
  Expires: Mon, 01 Jan 1990 00:00:00 GMT
  Content-Type: application/json; charset=utf-8
}
Error: {
  "error": {
    "code": 403,
    "message": "The caller does not have permission",
    "status": "PERMISSION_DENIED"
  }
}
3
  • What is not working? Can you share the code that you've tried and also the errors that you get?
    – Dharmaraj
    Commented Mar 29, 2023 at 16:32
  • Do you have Email/Password authentication enabled in Firebase -> Authentication section?
    – Artur A
    Commented Mar 29, 2023 at 16:52
  • I want to automate that using api, I wanted to enable email/password authentication with an api, is that possible?
    – Samaritan
    Commented Mar 29, 2023 at 16:54

1 Answer 1

0

Yes, it should be possible.

REST API is language agnostic.

You should be able to find your API Key at the application settings in Firebase, whether it is mobile or a web app:

enter image description here

REST API documentation for Email/Password sign in: https://firebase.google.com/docs/reference/rest/auth/#section-sign-in-email-password

3
  • Hey Artur, actually what i wanted was to enable email/password firebase auth for the firbase project not creating an account for a user, is there an api for that as well?
    – Samaritan
    Commented Mar 29, 2023 at 16:53
  • 1
    @Samaritan I see, in that case you can try to investigate IAM roles for you API key, some instructions are at cloud.google.com/docs/authentication/rest, it looks like you miss some roles. In theory it should be possible as Firebase is just a wrapper on top of Google cloud. From other side, Firebase may store some specific flag settings and a raw update via REST may break some flow. There are guys from Google Firebase team who monitor questions, they can provide more details.
    – Artur A
    Commented Mar 29, 2023 at 17:00
  • 1
    One more possibility is to call firebase cli from C#. Another option is to use internal firebase REST API, for example github.com/firebase/firebase-tools during firebase login calls auth.firebase.tools/login?code_challenge=.... Not an easy one, but in theory is possible.
    – Artur A
    Commented Mar 29, 2023 at 17:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.