首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用服务帐户删除eMails

使用服务帐户删除eMails
EN

Stack Overflow用户
提问于 2021-07-22 09:26:29
回答 1查看 210关注 0票数 0

我试图阅读和删除我的邮件为一些自动测试,我的问题是,我不能使用oauth,因为它需要用户的输入。所以我试着用一个服务帐户

但是当我运行我的代码时,我得到了一个错误。

现在我想知道我的代码或者我在Google控制台中的权限有什么问题吗?

错误:

“发生了一个或多个错误。”(错误:“unauthorized_client”,描述:“客户端未经授权使用此方法检索访问令牌,或者客户端未授权任何所请求的范围。”,Uri:“”)

发生在这里:credential.RequestAccessTokenAsync(CancellationToken.None).Result

代码语言:javascript
复制
        private ServiceAccountCredential credential;
        private GmailService Service { get; set; }

        // If modifying these scopes, delete your previously saved credentials
        private static string[] Scopes = { GmailService.Scope.MailGoogleCom };
        private static string applicationName = "EmailReader";
        private static string credentialsFile = "emailreader-289206-c91027110691.p12";
        private static string keyPassword = "notasecret";
        private static string serviceAccountEmail = "auristesterdienstkonto@emailreader-289206.iam.gserviceaccount.com";
        private static string userEmail = "auristester1@gmail.com";

        private Func<Message, bool> lastFilter;

        public GmailLibary() //my constructor
        {
            var certificate = new X509Certificate2(
                credentialsFile,
                keyPassword,
                X509KeyStorageFlags.Exportable);

            credential = new ServiceAccountCredential(
                new ServiceAccountCredential.Initializer(serviceAccountEmail)
                {
                    User = userEmail,
                    Scopes = Scopes
                }.FromCertificate(certificate)
            );

            if (credential.RequestAccessTokenAsync(CancellationToken.None).Result)
            {
                Service = new GmailService(
                    new BaseClientService.Initializer()
                    {
                        ApplicationName = applicationName,
                        HttpClientInitializer = credential
                    }
                );
            }
        }

权限

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-22 11:37:14

“客户端未经授权使用此方法检索访问令牌,或未对所请求的任何范围授权的客户端。”

通常意味着您正在使用服务帐户的代码,但还没有在Google云控制台上创建服务帐户凭据。确保您所设置的服务帐户也出现在谷歌云控制台上,我不确定在Google上创建的服务帐户是否是相同的。(不是说我已经测试过了。)

对于Gmail来说,这也是由于您没有在google工作区帐户中正确地设置域广域委托。请记住,服务帐户只与gmail一起使用您的域名电子邮件,您不能使用它与标准的gmail电子邮件。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68482398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档