【问题标题】:Error [403] in trying to get list of emails when using Gmail api使用 Gmail api 时尝试获取电子邮件列表时出现错误 [403]
【发布时间】:2021-12-11 02:04:39
【问题描述】:

我正在尝试做一个非常简单的任务。使用 Google.Apis 的 api 获取电子邮件列表。 这是我得到的错误:

Google.Apis.Requests.RequestError
   Delegation denied for association8385@gmail.com [403]
   Errors [
  Message[Delegation denied for association8385@gmail.com] Location[ - ] Reason[forbidden] 
  Domain[global] ]

这是我的代码:

    using (FileStream stream = new(emailSettings.Value.ClientCredentials, FileMode.Open, 
           FileAccess.Read))
    {

       string folderPath =  emailSettings.Value.CredentialsInfo;
       string filePath = Path.Combine(folderPath, "APITokenCredentials");

       _credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.FromStream(stream).Secrets,
                _scopes, 
                "user", 
                CancellationToken.None, 
                new FileDataStore(filePath, true)).Result;

    }
    _service = new GmailService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = _credential,
        ApplicationName = emailSettings.Value.ApplicationName
    });

    UsersResource.MessagesResource.ListRequest listRequest = 
         _service.Users.Messages.List(_hostAddress);
        listRequest.LabelIds = "INBOX";
        listRequest.IncludeSpamTrash = false;
        listRequest.Q = "is:unread";
        ListMessagesResponse listReqponse = listRequest.Execute() ;

当我尝试执行 listRequest.Execute()

时发生崩溃

我已启用 Gmail API 和 People Api。我已创建 ClientCredentials json。我还创建了我的 OAuth 2.0 客户端 ID 作为 Web 应用程序。创建服务时,会创建必要的令牌。对于这个简单的任务,我怀疑这是一个配置问题。

谁能给我指出正确的方向

【问题讨论】:

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


    【解决方案1】:

    您收到的错误消息有点奇怪,这表明您正在尝试使用服务帐户进行身份验证。然而,您使用的代码是用于已安装的应用程序,您提到您正在尝试使用 Web 应用程序凭据,不幸的是您正在使用的代码是用于已安装的应用程序。

    我刚刚使用来自谷歌开发者控制台的已安装客户端运行了您的代码,“并进行了一些修复”,这很有效。

    class Program
        {
    
            private static readonly string creds = @"C:\YouTube\dev\credentials.json";
            private static readonly string credsPath ="creds";
            
            static void Main(string[] args)
            {
                UserCredential _credential;
                
                using (var stream = new FileStream(creds, FileMode.Open, FileAccess.Read))
                {
    
                    var filePath = Path.Combine(credsPath, "APITokenCredentials");
    
                    _credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.FromStream(stream).Secrets,
                        new []{ Google.Apis.Gmail.v1.GmailService.Scope.GmailModify}, 
                        "user", 
                        CancellationToken.None, 
                        new FileDataStore(filePath, true)).Result;
    
                }
                var _service = new GmailService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = _credential,
                    ApplicationName = "emailSettings.Value.ApplicationName"
                });
    
                var request = _service.Users.Messages.List("me");
                request.LabelIds = "INBOX";
                request.IncludeSpamTrash = false;
                request.Q = "is:unread";
                var response = request.Execute() ;       
    
            }
    

    如果您尝试使用 asp .net core 或 .net 5 创建 Web 应用程序,那么您应该遵循类似的内容

    Asp .net core google login我没有 gmail 的示例,但如果您需要,我可以将它们放在一起。

    【讨论】:

      【解决方案2】:

      根据我的研究,我目前的方法不适用于我必须完成的其他任务。我相信我应该使用服务帐户。 所以对于这个这个问题已经回答了。 谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-02
        • 2016-11-30
        • 2015-12-12
        • 1970-01-01
        • 2020-08-04
        • 2015-12-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多