Connection with Google Drive API

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

1 Like

Interested to know more on the same… @Rammohan91 Would you be able to debug this and make it a package ?

Definitely. Let me try this and i ll update. :slight_smile:

Hey @javirk,

I tried to replicate your custom activity.

Follow as Below to resolve the issue:

  1. Rename the below files under your path (…\UiPath\app-18.1.3)

image

  1. Install your custom activity
  2. Install Google Drive Api (NuGet Gallery | Google.Apis.Drive.v3 1.57.0.2789)
  3. Install Google Api (NuGet Gallery | Google.Apis 1.57.0)

Note: Make sure that your ‘client_secret.json’ is available under the project folder which has your workflow.

Here is the package that i used.
GoogleDriveAPITest_Activities.1.0.1.nupkg (5.0 KB)

Let me know if you still face an issue.

Thanks,
Rammohan B.

3 Likes

It works! Have you made any changes on the package?

You’re a boss. Thank you!

1 Like

No. I haven’t made any changes to the packages. Its exactly the same as you added above. It just seems to be issue with uipath referencing the older version of apis present.

Thanks a lot. Learned something new. Happy to help. :slight_smile:

Thanks,
Rammohan B.

Good Job @Rammohan91 and much appreciated.

1 Like

Hi, would it be possible to share an example project here that used it so I can test it myself?

I just cant understand how to use all this in Uipath together and actually show a working example.

Hi, you only have to follow the steps @Rammohan91 wrote. From number 2, where your custom activity is GoogleDriveAPITest_Activities.1.0.1.nupkg (he attached it to his post).

Also, you must turn on Drive API, as stated here: JavaScript quickstart  |  Drive API  |  Google Developers

Once you have installed everything, the google drive api will appear on the left of Uipath, along with all the activities.