Error [403] in trying to get list of emails when using Gmail api

Viewed 223

I am trying to do a very simple task. Getting a list of Emails using the api of Google.Apis. This is the error that I am getting:

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

Here is my code:

    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() ;

The crash occurs when I try to execute listRequest.Execute()

I have enabled the Gmail API and People Api.I have created the ClientCredentials json. Also I have created my OAuth 2.0 Client IDs as a Web Application. When the service is created the necessary token is created. For this simple task I suspect that it is a configuration issue.

Can somebody point me in the right direction

2 Answers

The error message you are getting is a little strange it implies that you are trying to use a service account to authenticate. Yet the code you are using is for an installed application, you mention that you are trying to use a web application credentials unfortunately the code you are using is for an installed application.

I just ran your code "with a few fixes" using an installed client from google developer console and this works.

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() ;       

        }

If you are trying to create a web app using asp .net core or .net 5 then you should be following something like this

Asp .net core google login I dont have a sample for gmail but i can put one together if you need.

Based on my research my current approach will not be work with the other tasks that I have to do. I believe the I should be using the Service account. So for this this question has been answered. Thanks

Related