c# - Google API (Gmail) 因先决条件失败 400 而失败

标签 c# google-api gmail-api google-api-dotnet-client google-identity

我正在尝试使用 google 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 设置中,我使用的帐户具有完全权限:

enter image description here

该帐户具有完整权限:

enter image description here

同样,更完整的权限:

enter image description here

我错过了什么?我找不到任何仅连接到收件箱就有意义的代表性 .Net 请求。

最佳答案

我遇到了同样的问题。上传到 Google Drive (Sheets) 的 XLSX 文档不支持 Google Sheets API。将数据复制到直接在 Google 表格中创建的表格中解决了该问题。

关于c# - Google API (Gmail) 因先决条件失败 400 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39255729/

相关文章:

python - 如何在没有网络浏览器的情况下使用 google drive API

c# - 在 ASP.NET Core 中,有没有办法从 Program.cs 设置中间件?

java - 如何使用 gmail api 获取访问 token

c# - 跨聚合根搜索 child

android - Google Spreadsheet API update\edit with protocol

c# - Gmail API - 值 '= Get Labels google-api-dotnet-client/1.25.0.0 (gzip)' 的格式无效

java - 'com.google.api.client.json.jackson2.JacksonFactory' 已弃用。我有哪些选择?

api - 调用Gmail API时如何使用fields参数

c# - 使用 UploadFromStreamAsync 上传文件后 Azure blob 文件损坏

c# - 检查屏幕上是否显示 Vector3