【问题标题】:Google API (Gmail) failing with failed precondition 400Google API (Gmail) 因失败的前提条件 400 而失败
【发布时间】:2017-01-08 09:43:46
【问题描述】:

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

帐户拥有完全权限:

同样,更完整的权限:

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

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我以前从未在 IAM 中看到授予域范围委派的方法。

    通常情况下,开发者 ID 会在域 CPanel 中列入白名单,例如 described in these docs。

    (“那么 Google Apps 域的管理员必须完成以下步骤”的段落。)

    【讨论】:

      【解决方案3】:

      指定您要为其获取消息的用户。在你的情况下,它看起来像这样:

      credential = GoogleCredential
                      .FromStream(stream)
                      .CreateScoped(new[]{ GmailService.Scope.GmailModify })**
                      .CreateWithUser("me") **;
      

      【讨论】:

        猜你喜欢
        • 2018-08-12
        • 2020-05-08
        • 2022-11-09
        • 1970-01-01
        • 1970-01-01
        • 2020-11-28
        • 2012-02-15
        • 2013-08-25
        • 1970-01-01
        相关资源
        最近更新 更多