Gsuit/Google Drive API integration in .NET MAUI
Tags: dotnet, maui, google, api
Published: 2023-10-22 09:50:52

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](https://dev.to/jaymalli_programmer/google-oauth-20-authorization-service-implementation-in-net-maui-okl)

=> 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](https://console.cloud.google.com)


![API Services](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22yte2f5ehjj7893yp7y.png)


![Library](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2heafouev16fhh85ac9c.png)


![Google Drive API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1g4um77ccjbtx1lykjou.png)

After that go to [Admin Console](https://admin.google.com).

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1ovw61i32hqik7ivlbp.png)

Add new API Client by click on "Add new"


![Admin Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m56yt1dea947y1xp6gm8.png)

Provide the Client Id which was created previously which can be get from [Google cloud console](https://console.cloud.google.com) under the credentails > Oauth Client Id tab.

If you want to know that how to create client Id then check this previous [blog](https://dev.to/jaymalli_programmer/google-oauth-20-authorization-service-implementation-in-net-maui-okl)

And provide this Oauth Scope for Drive access: 

Authorize the provided client Id to access the google drive. 

![Admin Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5z1ebqsxgm9tsi4so3bd.png)

Makke sure  that Drive & Docs Service is ON from the [Admin  Console](https://admin.google.com/)
 

![Admin Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnw2706g5clsuaocqlqg.png)

Now it's time to do some code!

=> Required Nuget Packages :
- [Google.Apis --v 1.60](https://www.nuget.org/packages/Google.Apis)
- [Google.Apis.Auth --v 1.60](https://www.nuget.org/packages/Google.Apis.Auth)
- [Google.Apis.Drive.v3 --v 1.60](https://www.nuget.org/packages/Google.Apis.Drive.v3)
- [Google.Apis --v 1.60](https://www.nuget.org/packages/Google.Apis)
-[Newtonsoft.Json]  (https://www.nuget.org/packages/Newtonsoft.Json)

The basic code for the Auth serivce is available in the below repository which we had done in previous blog.
[Github Repository](https://github.com/JayMalli/Google_OAuth2.0-MAUI)

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;
        }
    }
}

```

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

[GithHub Repository](https://github.com/JayMalli/GSuit_Drive-MAUI/tree/main)

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](https://www.linkedin.com/in/jaymalli)

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