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"
}
}