我想在 .NET 中使用 REST API 启用电子邮件/密码登录方法。它使用应用程序默认凭证来授权身份 REST API 调用。我定义了在请求正文中传递参数所需的类型。但我不断收到无效的论点。
我实现的代码如下所示:
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";
// https://www.googleapis.com/drive/v3/files
public override string BasePath {get;}
public override string BaseUri {get;}
public override IList<string> Features => throw new NotImplementedException();
}
但是运行这段代码时出现错误:
{
"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."
}
]
}
]
}
}
我按照文档提供了正确的请求正文,但错误仍然存在,谁能帮我解决这个问题。