Invoke code for GMAIL in Csharp is not working

Hi All,

This code is working perfectly fine in VS code and getting the emails from Gmail account.
But when i add the same in UiPath Studio in Invoke code its not working fine.

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;

class GmailExample
{
static string Scopes = { GmailService.Scope.GmailReadonly };
static string ApplicationName = “Gmail API .NET Quickstart”;

static void Main(string[] args)
{
    UserCredential credential;

    // The file token.json stores the user's access and refresh tokens, and is created automatically when the
    // authorization flow completes for the first time.
    using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
    {
        // The file token.json stores the user's access and refresh tokens, and is created automatically when the
        // authorization flow completes for the first time.
        string credPath = "token.json";
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.FromStream(stream).Secrets,
            Scopes,
            "user",
            CancellationToken.None,
            new FileDataStore(credPath, true)).Result;
        //Console.WriteLine("Credential file saved to: " + credPath);
    }

    // Create the Gmail API service.
    var service = new GmailService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName,
    });

    // Retrieve a list of labels in the user's mailbox.
    UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List("me");
    request.LabelIds = "INBOX";
    request.Q = "is:unread"; // Optional query for unread messages

    ListMessagesResponse messages = request.Execute();
    Console.WriteLine("Messages:");
    if (messages.Messages != null)
    {
        foreach (var message in messages.Messages)
        {
            var msg = service.Users.Messages.Get("me", message.Id).Execute();
            Console.WriteLine("- " + msg.Snippet);
        }
    }
    else
    {
        Console.WriteLine("No new messages.");
    }
}

}

Any help or working code to read the gmails using Invoke code with Csharp or VB.NET will work fine.

Ultimate goal is to read the emails and get the content of the messages as Gmail Integration and other options are not available or won’t work.

I have the credentials json as well as my app is authorized in Console.google account and a project is created on Google.

Thanks

@Anil_choudhary

What error are you getting?

Also you can use google workspace acope activities which support your current authentication as wel…that would be more easier to use

Cheers

Well as mentioned in my request: Can’t use Google workspace activities due to the dynamic nature of the requirements.
The use case is: the same code should allow multiple gmails accounts messages to be read and the gmail accounts can be dynamic, so we have to create a credentials json and use it for each user.

I m passing the API key as well as tried oAuth2 code to check with passing the client secrets json file which is again not working.

Any documentation would be really helpful

IN Addition to the same:

The json Google gave me when i created the project is not recoganized or read properly by the code.

@Anil_choudhary

Is the json password protected?

If so can you remove the pssword and try…as password is supported for p12 type but not json type

Cheers

Perhaps this helps: Google Cloud - Converting Service Account Credentials from P12 to Json Format - John Hanley

Well i tried all mentioned steps but not sure whats missing in the code. Not sure whats wrong. MIght be need to create a fresh account and try it again from begining