I’m working on a Dart/Flutter application where I’m currently manually downloading Google Sheets JSON credentials and using a hardcoded sheet ID to perform operations like reading and writing data to Google Sheets. Here’s an example of my current implementation:
var sheetId = "1JlK0czrgD8yM3i9FaIz8Iz8vJwLrUOzYcv3pY";
var credentials = r'''{
"type": "service_account",
"project_id": "",
"private_key_id": "",
"private_key": "-----BEGIN PRIVATE KEY-----nn-----END PRIVATE KEY-----n",
"client_email": "",
"client_id": "",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/",
"universe_domain": "googleapis.com"
}''';`
final gsheetinit = GSheets(credentials);
var gSheetController;
Worksheet? gSheetCrudUserDetails;`
GsheetInit()async{
gSheetController = await gsheetinit.spreadsheet(sheetId);
gSheetCrudUserDetails = await gSheetController.worksheetByTitle('gsheet_demo');
}`
Future main()async{
WidgetsFlutterBinding.ensureInitialized();
await GsheetInit();
runApp(const MyApp1());
}
What I Tried:
So far, I’ve been using a service account with manually downloaded JSON credentials and a hardcoded sheet ID to perform Google Sheets operations using the GSheets package in Dart/Flutter.
I expected this method to allow me to read/write data to Google Sheets easily for a single user or sheet, and it worked as expected.
However, I want to scale this for multiple users. I tried to investigate ways to programmatically generate credentials in dart or use a common service account, but I couldn’t find a clear solution to allow each user to have their own Google Sheet.
What I Expected:
I was hoping to find a way to generate separate Google Sheets credentials and sheet IDs for each user programmatically as they log in to the app, so that each user gets their own private sheet for storing data. Ideally, I want the app to automatically create these credentials and sheets without manual intervention, and securely manage authentication.