我正在尝试使用谷歌 API 来阅读电子邮件,但我一再未能获得良好的结果。它应该是在后台定期运行的服务器-> 服务器帐户,但我无法连接。代码是基本的:
GoogleCredential credential;
using (var stream = new FileStream("Content/service_credential.json", FileMode.Open,
FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream);
credential = credential.CreateScoped(new[] { GmailService.Scope.GmailModify });
}
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "try-apis",
});
ListLabelsResponse response = service.Users.Labels.List("me").Execute();
foreach (Label label in response.Labels.OrderBy(p => p.Name))
{
Console.WriteLine(label.Id + " - " + label.Name);
}
Console.Read();
错误:
Additional information: Google.Apis.Requests.RequestError
Bad Request [400]
Errors [
Message[Bad Request] Location[ - ] Reason[failedPrecondition] Domain[global]
]
在 IAM 设置中,我使用的帐户具有完全权限:
该帐户具有完全权限:
同样,更完整的权限:
我错过了什么?我找不到任何仅在连接到收件箱时有意义的代表性 .Net 请求。