Google API (Gmail) failing with failed precondition 400

时间:2016-08-31 17:33:38

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

I am trying to use the google APIs to read emails and I'm repeatedly failing to get good results. It is supposed to be a server->server account that runs periodically in the background, but I can't get it to connect. The code is basic:

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();

Error:

Additional information: Google.Apis.Requests.RequestError

Bad Request [400]

Errors [

    Message[Bad Request] Location[ - ] Reason[failedPrecondition] Domain[global]

]

In IAM settings, the account I'm using has full permissions:

enter image description here

The account has full permissions:

enter image description here

Likewise, more full permissions:

enter image description here

What am I missing? I can't find any representative .Net requests that make sense in just connecting to an in box.

3 个答案:

答案 0 :(得分:0)

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

通常,开发人员ID在域CPanel中列入白名单,如described in these docs

(段落中写着“然后,Google Apps域管理员必须完成以下步骤”。)

答案 1 :(得分:0)

我有同样的问题。上传到Google云端硬盘(表格)的XLSX文档不支持Google表格API。将数据复制到直接在Google表格中创建的表格中即可解决此问题。

答案 2 :(得分:0)

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

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