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