Gmail Mail Api Working in Api explorer but not in code c#

时间:2018-07-24 10:21:56

标签: c# gmail-api

I am using Gmail Api to get all the unread message in inbox , do some operations on it and then mark the mail as READ.

However when i do any of the below:

  var markAsReadRequest = new ModifyThreadRequest { RemoveLabelIds = new[] { "UNREAD" } };
  service.Users.Threads.Modify(markAsReadRequest, "xxx@gmail.com", email.ThreadId)
                                                                        .Execute();

  var markemail=new ModifyMessageRequest { RemoveLabelIds = new[] { "UNREAD" } };
  service.Users.Messages.Modify(markemail, "xxx@gmail.com", email.Id)
                                                                     .Execute();

I am getting Error:

Google.Apis.Requests.RequestError

Insufficient Permission [403] Errors [ Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global] ]

The thing is i open Api explorer and run the same request and it works fine. So not sure what am i doing wrong here.

gmail.users.threads.modify:

Modifies the labels applied to the thread. This applies to all messages in the thread.

This works and marks the email as UNREAD.

1 个答案:

答案 0 :(得分:1)

发现问题必须将其添加到顶部:

  static string[] Scopes = { mailService.Scope.GmailReadonly,GmailService.Scope.GmailModify };