I wanted to enable email/password sign-in method using REST API in .NET. It uses application default credential to authorize the identity REST API call. And I defined the required types to pass the parameters in the request body. But I keep getting invalid argument.
The code I implemented is shown below :
public static async void EnableEmailPasswordAuth() { // Set up the Google API client library. var credential = await GoogleCredential.GetApplicationDefaultAsync(); var initializer = new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "myAppName", }; var service = new MyCustomService(initializer); var req2 = new UpdateConfigRequest { Config = new Config { Email = new EmailConfig { Enabled = true }, Password = new PasswordConfig { Enabled = true } } }; string requestBodyJson = JsonConvert.SerializeObject(req2); var RequestUri = new Uri("https://identitytoolkit.googleapis.com/admin/v2/projects/myProjectId/config"); StringContent content2 = new StringContent(requestBodyJson, System.Text.Encoding.UTF8, "application/json"); var response = await service.HttpClient.PatchAsync(RequestUri, content2); // Deserialize the response content string responseContent = await response.Content.ReadAsStringAsync(); var result = NewtonsoftJsonSerializer.Instance.Deserialize<dynamic>(responseContent); // Output the result Console.WriteLine(result); } public class PasswordConfig { public bool Enabled { get; set; } public bool PasswordRequired { get; set; } } public class EmailConfig { public bool Enabled { get; set; } public bool PasswordRequired { get; set; } } public class Config { public PasswordConfig Password { get; set; } public EmailConfig Email { get; set; } } public class UpdateConfigRequest { public Config Config { get; set; } } public class MyCustomService : BaseClientService { // Constructor public MyCustomService( BaseClientService.Initializer initializer) : base(initializer) { } // Required properties public override string Name => "my-custom-service"; public override string BasePath {get;} public override string BaseUri {get;} public override IList<string> Features => throw new NotImplementedException(); }
But I get error when running this code :
{ "error": { "code": 400, "message": "Invalid JSON payload received. Unknown name \"Config\" at 'config': Cannot find field.", "errors": [ { "message": "Invalid JSON payload received. Unknown name \"Config\" at 'config': Cannot find field.", "reason": "invalid" } ], "status": "INVALID_ARGUMENT", "details": [ { "@type": "type.googleapis.com/google.rpc.BadRequest", "fieldViolations": [ { "field": "config", "description": "Invalid JSON payload received. Unknown name \"Config\" at 'config': Cannot find field." } ] } ] } }
I followed the documentation [ reference ] and provided the right request body but the error persists, can anyone help me with this.
Hi @samrawit,
Welcome to Google Cloud Community!
The reason behind this is that the API expects the field names in the request body to be in lowercase. This is because the API's request body is defined using lowercase field names, and when the API receives a request with a field name in uppercase, it is not able to find the corresponding field in the request body and returns an error.
In your case, the Update an Identity Toolkit project configuration REST API is expecting the "config" field in the request body, but your request body had "Config" (with a capital C) instead. So by changing it to "config" (with a lowercase c), the API was able to locate the field in the request body and process the request successfully.
Thank you