Google API (Gmail) failing with failed precondition 400

北城以北 提交于 2019-12-11 02:11:27

问题


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:

The account has full permissions:

Likewise, more full permissions:

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


回答1:


I've never seen a way in IAM to grant domain-wide delegation before.

Typically the developer ID is whitelisted in the domain CPanel like described in these docs.

(The paragraph that says "Then an administrator of the Google Apps domain must complete the following steps".)




回答2:


I had this same issue. XLSX documents uploaded into Google Drive (Sheets) do not support Google Sheets API. Copying the data into a sheet created directly in Google Sheets resolved the issue.




回答3:


Specify the user that you want to get messages for. In your case, it'd look like this:

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


来源:https://stackoverflow.com/questions/39255729/google-api-gmail-failing-with-failed-precondition-400

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!