c# - 当我在 c# 中使用 gmail API 修改消息标签时出现问题

标签 c# .net google-api gmail-api google-api-dotnet-client

我正在尝试删除消息标签。我能够成功阅读邮件,但是当我尝试修改邮件标签时出现问题

An error occurred: Google.Apis.Requests.RequestError Insufficient Permission [403] Errors Message[Insufficient Permission] Location[ - ] Reason[insufficientPermis sions] Domain[global]

我不得不尝试从 json 创建服务,但它有同样的问题。 这是我的代码

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets
                {
                    ClientId = clientId,
                    ClientSecret = clientSecret,
                },
                new[] { GmailService.Scope.MailGoogleCom, GmailService.Scope.GmailModify, GmailService.Scope.GmailCompose },//new[] { GmailService.Scope.GmailModify, GmailService.Scope.GmailCompose, GmailService.Scope.GmailReadonly },
                "user",
                CancellationToken.None).Result;

        var service = new GmailService(new BaseClientService.Initializer
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

然后调用api修改消息中未读标签

ModifyMessageRequest mods = new ModifyMessageRequest();
mods.AddLabelIds = null;
mods.RemoveLabelIds = new List<string> { "UNREAD" });
service.Users.Messages.Modify(mods, userId, messageId).Execute(); 

最佳答案

你需要修复你的范围或者添加

https://www.googleapis.com/auth/gmail.labels Create, read, update, and delete labels only.

或者只是请求

https://mail.google.com/ Full access to the account, including permanent deletion of threads and messages. This scope should only be requested if your application needs to immediately and permanently delete threads and messages, bypassing Trash; all other actions can be performed with less permissive scopes.

removelablesids 还请求一个标签 ID

removeLabelIds[]  A list IDs of labels to remove from this message.

我不认为 new List { "UNREAD"}); 会返回标签 ID。尝试做 lables.list找到标签和 ID

关于c# - 当我在 c# 中使用 gmail API 修改消息标签时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32834455/

相关文章:

c# - .NET:通过项目配置不同的目标框架

c# - 如何统一显示网格线?

c# - 从 Oracle 到 MySql 数据库的 DateTime 值

c# - 将数据表转换为泛型类型列表

c# - 最佳实践 如何使用 .net CORE 隔离动态代码/程序集

javascript - 在代码中包含客户端 ID 和客户端密码是否存在安全风险?

安卓 GooglePlus API 错误

c# - 如何使用 JSON.NET 遍历嵌套字典?

.net - 使用 WCF 和 DefaultCredentials 时如何处理密码更改?

node.js - 通过 Node 将浏览器麦克风流连接到 Google Speech Api