Hi,
I’m trying to connect UiPath with Google Drive API through a custom activity written in C#. I’ve made custom activities to work before, but I’m having some problems with Google. UiPath gives me the error:
Could not load type 'Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker' from assembly 'Google.Apis.Auth, Version=1.13.1.0,'.
This is the code I have in C# that works fine when executed in visual studio:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Activities;
using System.ComponentModel;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System.IO;
using System.Threading;
using System.Diagnostics;
namespace ClassDriveCustom
{
public class SubeDrive : CodeActivity
{
[Category("Input")]
[RequiredArgument]
public InArgument<string> Ruta { get; set; }
//[Category("Input")]
//public InArgument<double> SecondNumber { get; set; }
[Category("Output")]
public OutArgument<string> Resultado { get; set; }
protected override void Execute(CodeActivityContext context)
{
string[] Scopes = { DriveService.Scope.Drive };
string ApplicationName = "Drive UiPath 2";
UserCredential credential;
using (var stream = new FileStream("client_id.json", FileMode.Open, FileAccess.Read))
{
//string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
//credPath = Path.Combine(credPath, "client_id.json");
string credPath = "client_id.json";
Debug.WriteLine(credPath);
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
//Console.WriteLine("Credential file saved to: " + credPath);
}
//credential = GetCredentials();
var resultado = "";
//Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
////UPLOAD
var imagen = Ruta.Get(context);
var fileMetadata = new Google.Apis.Drive.v3.Data.File() { Name = imagen };
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream(imagen, System.IO.FileMode.Open))
{
request = service.Files.Create(fileMetadata, stream, "image/jpeg");
request.Fields = "id";
request.Upload();
}
var file = request.ResponseBody;
try
{
Console.WriteLine("File ID: " + file.Id);
resultado = "ok";
}
catch
{
resultado = "nok";
}
//resultado = UploadBasicImage(imagen, service);
Console.WriteLine("Done");
Console.Read();
//var secondNumber = SecondNumber.Get(context);
//var result = System.Math.Pow(firstNumber + secondNumber, 2);
resultado = imagen;
Resultado.Set(context, resultado);
}
The code works fine in Visual Studio, it’s only when I try to open it in uipath that i get the error.
In UiPath, I installed Google.Apis.Drive.v3, which is the package the code needs to work.
Thank you in advance!
Javier