Connection to Google sheets API from .net Maui application

288 views Asked by At

Greetings to all of you.

I have This code working fine to connect to Google sheets API from WPF (Desktop Application C# ) application.

My question: Is it possible to modify it to work in .net Maui application( Application Mobile , Desktop C#)

My code:

//connection Google sheets API

string[] Scopes = { SheetsService.Scope.Spreadsheets };
var service = new SheetsService(new BaseClientService.Initializer() { HttpClientInitializer = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = " My Client ID", ClientSecret = "My Client secret" }, Scopes, "user", CancellationToken.None, new FileDataStore("MyAppsToken")).Result, ApplicationName = " My Application ", });
   var values = service.Spreadsheets.Values.Get("My Spread Sheets ID", $"{"My sheet Name"}!A:C").Execute().Values;

I've tried changing the settings Google console from Application Desktop to web application , but to no avail.

1

There are 1 answers

0
Govind On

You can use the following code for MAUI Windows APP, this will not work for android due to loop-back flow issue from google:

private static string[] SCOPES =
{
    SheetsService.Scope.Spreadsheets
};

SheetsService service = new SheetsService(new
    BaseClientService.Initializer()
    {
        HttpClientInitializer =
    GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets()
            {

                ClientId = WIN_CLIENT_ID,
                //This should be you client id from json

                ClientSecret = WIN_CLIENT_SECRET,
                //this should be your secret key

            }, SCOPES, "user", CancellationToken.None, new FileDataStore("MyAppToker")).Result, ApplicationName = APP_NAME
    });