我尝试使用身份工具包https://identitytoolkit.googleapis.com/v2/projects/{projectId}/config?key={apiKey}的rest API补丁请求,我将项目ID替换为我在控制台上的项目ID,并将apikey替换为firebase web api密钥,但它不起作用。我如何使用此请求来启用电子邮件/密码firebase身份验证?
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}");
}
错误:
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"
}
}
1条答案
按热度按时间idfiyjo81#
是的,应该可以。
REST API与语言无关。
您应该能够在Firebase中的应用程序设置中找到您的API密钥,无论是移动的应用程序还是Web应用程序:
用于电子邮件/密码登录的REST API文档:https://firebase.google.com/docs/reference/rest/auth/#section-sign-in-email-password