DEV Community

Jay Malli
Jay Malli

Posted on • Edited on

Gsuit/Google Drive API integration in .NET MAUI

Hello Developers, we had implemented google OAuth 2.0 service for authoring user with google sign in the previous blog.

If you couldn't get the chance to check that blog then check it out, Hurry up! I'm waiting for you here!!!

=> Previous Blog

=> A Quick flashback of our previous Blog :

  • Google Developer Account >> Create Oauth Client Id >> implement OAuth service >> Get the access__token & refresh_token.

Now, It's time to use that tokens in some useful way. So we are integrating drive API in our app to get the user's drive data.

So, First enable the Google Drive API from the API Services of Google cloud console

API Services

Library

Google Drive API

After that go to Admin Console.

Go to Security -> Access And Data Control -> API controls -> Domain-Wide delegation.

Image description

Add new API Client by click on "Add new"

Admin Console

Provide the Client Id which was created previously which can be get from Google cloud console under the credentails > Oauth Client Id tab.

If you want to know that how to create client Id then check this previous blog

And provide this Oauth Scope for Drive access:

Authorize the provided client Id to access the google drive.

Admin Console

Makke sure that Drive & Docs Service is ON from the Admin Console

Admin Console

Now it's time to do some code!

=> Required Nuget Packages :

The basic code for the Auth serivce is available in the below repository which we had done in previous blog.
Github Repository

Here , we are implementing the feature of getting files from the drive which contains specific keywords.

=> code for Drive.cs

using Google.Apis.Drive.v3;
using File = Google.Apis.Drive.v3.Data.File;

namespace gsuit_api_maui
{

    public class Drive
    {
        public Drive()
        {
        }

        public List<File> GetDriveFiles(string[] keywords, DriveService driveService)
        {
            List<File> Files = new List<File>();
            foreach (string keyword in keywords)
            {
                try
                {
                    FilesResource.ListRequest files = driveService.Files.List();
                    files.Fields = "nextPageToken,files(id,name,mimeType,webViewLink,permissions)";
                    files.Corpora = "user";
                    files.IncludeItemsFromAllDrives = true;
                    files.IncludeTeamDriveItems = true;
                    files.SupportsAllDrives = true;
// filter for  files which contains specific keyword
                    files.Q = $"fullText contains '\"{keyword}\"'";
                    List<File> result = (List<File>)files.Execute().Files;
                    Files.AddRange(result);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

            }
            return Files;
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

You can find the necessary code in the repository mentioned below.

GithHub Repository

Thank you for joining me on this journey of discovery and learning. If you found this blog post valuable and would like to connect further, I'd love to connect with you on LinkedIn. You can find me at LinkedIn

If you have thoughts, questions, or experiences related to this topic, please drop a comment below.

Top comments (0)