Do you have the same idea with me that developing a lite Maui app shared among family member?
It means that App need handle storing the data on the internet /cloud
There is too much cost if we choose Azure Service or Other cloud service.
So what we can do?
I think it is a good idea to store the data in google sheet when we are developing small Maui App.
1 .Authentication with Google Cloud Console
you’ll need a JSON credential file from the Google Cloud console. This involves creating a service account and sharing your Google Drive files with it, granting the necessary permissions. This step is critical for the API to access your spreadsheets.
put json file in the resource folder or subfolder and There is much information in the Json file.

2 .Using Google Sheets & Google Drive Nuget Packages In Project
<PackageReference Include="Google.Apis.Sheets.v4" Version="1.67.0.3365" />
3 Access Google sheet in Maui APP
we can create a static class GoogleAuthentication to handle Access google sheet.
public static GoogleCredential LoginCredential()
{
GoogleCredential credential = ReadTextFile($"googlesheetcredentials.json").Result;
return credential;
}
4 Handling data reading/ writing in Google Sheet as database
First, we defined a class implemented interface to implement all the operation on google sheet , such query(get), update, delete.
Here is the code of query one single value
public GoogleSheetsManager(GoogleCredential credential)
{
_credential = credential;
}
public ValueRange GetSingleValue(string googleSpreadsheetIdentifier, string valueRange)
{
if (string.IsNullOrEmpty(googleSpreadsheetIdentifier))
throw new ArgumentNullException(nameof(googleSpreadsheetIdentifier));
if (string.IsNullOrEmpty(valueRange))
throw new ArgumentNullException(nameof(valueRange));
using (var sheetsService = new SheetsService(new BaseClientService.Initializer() { HttpClientInitializer = _credential }))
{
var getValueRequest = sheetsService.Spreadsheets.Values.Get(googleSpreadsheetIdentifier, valueRange);
return getValueRequest.Execute();
}
}
Then we need consume these method in maui App, we will define new class named Communication to handle all the request to Google sheet.
we will first check the internet connection to make sure we can get current manager, and Create a singleton instance for google sheet manager.
private GoogleSheetsManager CurrentSheetsManager
{
get
{
var checkResult = CheckInternetConnection().Result;
if (_sheetsManager == null)
{
if (checkResult)
{
var credential = GoogleAuthentication.LoginCredential();
_sheetsManager = new GoogleSheetsManager(credential);
return _sheetsManager;
}
return null;
}
if (checkResult)
{
return _sheetsManager;
}
return null;
}
}
we will handle all the request from view model in this class. Here is example :RegisterUserAsync .
public async Task<MessageResponse> RegisterUserAsync(PocketBookClient.Data.User user)
{
var checkResult = await CheckInternetConnection();
if(!checkResult)
{
return new MessageResponse { Success = false };
}
var spreadSheet = CurrentSheetsManager.GetSpreadsheet(spreadSheetId);
var newRecordRange = $"{UserSheet}!A:F";
var newRecord = new List<object>() { DateTime.Today.ToShortDateString(), user.Name, RSAService.EncryptData(user.Password), user.PhoneNumber, user.Email, user.GroupId };
try
{
var response = await CurrentSheetsManager.CreateSingleValueAsync(spreadSheet.SpreadsheetId, newRecordRange, newRecord);
return new MessageResponse { Success = true };
}
catch(Exception ex)
{
return new MessageResponse { Success = false };
}
}
Following these steps, we can create a very lite maui App shared the data on cloud easily.
Perry
Leave a reply to Parash Goswami Cancel reply