Flutter开发中,google_sign_in安装指引里的“register your application”指什么?
Hey there! I totally get the frustration of following official docs that push Firebase when you don't want to use it. Let's walk through how to set up Google Sign-In for your Flutter app, register your app properly without Firebase, and fetch Google Sheets data using the googleapis package.
Step 1: Register Your App in Google Cloud Console (No Firebase Needed)
Forget Firebase—we'll handle this directly in Google Cloud Console:
- Create a new project (or use an existing one).
- Navigate to API & Services > OAuth Consent Screen:
- Choose "External" (for public apps) or "Internal" (if only for your organization's users).
- Fill in basic details like your app name, support email, and any required contact info.
- If you picked "External", add test users (only these users can sign in until your app is verified by Google).
- Go to API & Services > Credentials:
- Click Create Credentials > OAuth client ID.
- Select your platform (Android, iOS, or Web):
- Android: Enter your app's package name (e.g.,
com.yourcompany.yourapp) and SHA-1 fingerprint. You can get the debug SHA-1 with this command:
Don't forget to add your release SHA-1 later if you plan to publish the app.keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android - iOS: Enter your app's Bundle ID, then note the URL Scheme provided (it looks like
com.googleusercontent.apps.[your-client-id]—you'll need this for Info.plist).
- Android: Enter your app's package name (e.g.,
- Once created, save your client ID—you'll need it for your Flutter project.
- Finally, enable the Google Sheets API in the Cloud Console (search for it under API & Services > Library, then click "Enable").
Step 2: Configure Your Flutter Project
First, add the required dependencies to your pubspec.yaml:
dependencies: flutter: sdk: flutter google_sign_in: ^5.4.0 googleapis: ^10.0.0 googleapis_auth: ^1.4.0 # Needed to create an authenticated client for Google APIs
Android Setup
Open android/app/src/main/AndroidManifest.xml and add this meta-data inside the <application> tag:
<meta-data android:name="com.google.android.gms.auth.api.signin.client_id" android:value="YOUR_ANDROID_CLIENT_ID"/>
iOS Setup
Open ios/Runner/Info.plist and add these entries:
<key>GIDClientID</key> <string>YOUR_IOS_CLIENT_ID</string> <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>com.googleusercontent.apps.[YOUR_CLIENT_ID]</string> </array> </dict> </array>
Step 3: Implement Sign-In & Fetch Google Sheets Data
Here's a complete example to handle sign-in and retrieve data from a Google Sheet:
import 'package:flutter/material.dart'; import 'package:google_sign_in/google_sign_in.dart'; import 'package:googleapis/sheets/v4.dart'; import 'package:googleapis_auth/auth.dart'; import 'package:googleapis_auth/auth_io.dart'; class GoogleSheetsDemo extends StatefulWidget { const GoogleSheetsDemo({super.key}); @override State<GoogleSheetsDemo> createState() => _GoogleSheetsDemoState(); } class _GoogleSheetsDemoState extends State<GoogleSheetsDemo> { final GoogleSignIn _googleSignIn = GoogleSignIn( scopes: [ SheetsApi.spreadsheetsReadonlyScope, // Use spreadsheetsScope for read/write access ], // Add clientId here only if you're targeting Web; mobile uses the Manifest/Info.plist config // clientId: "YOUR_WEB_CLIENT_ID", ); String? _sheetData; Future<void> _signInAndFetchSheets() async { try { // Trigger sign-in flow final GoogleSignInAccount? account = await _googleSignIn.signIn(); if (account == null) { setState(() => _sheetData = "Sign-in canceled."); return; } // Get authentication credentials final GoogleSignInAuthentication auth = await account.authentication; final AuthClient client = GoogleAuthClient(auth.accessToken!); // Initialize Sheets API client final sheetsApi = SheetsApi(client); // Replace with your Google Sheet ID and range const spreadsheetId = "YOUR_SPREADSHEET_ID"; const range = "Sheet1!A1:C10"; // Adjust to your desired range // Fetch data final response = await sheetsApi.spreadsheets.values.get(spreadsheetId, range); final values = response.values; if (values == null || values.isEmpty) { setState(() => _sheetData = "No data found in the sheet."); return; } // Format data for display String formattedData = ""; for (var row in values) { formattedData += "${row.join(", ")}\n"; } setState(() => _sheetData = formattedData); } catch (e) { setState(() => _sheetData = "Error: $e"); } } Future<void> _signOut() async { await _googleSignIn.signOut(); setState(() => _sheetData = null); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text("Google Sheets Demo")), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ if (_sheetData == null) ElevatedButton( onPressed: _signInAndFetchSheets, child: const Text("Sign in with Google & Fetch Data"), ) else ...[ Text(_sheetData!), const SizedBox(height: 20), ElevatedButton( onPressed: _signOut, child: const Text("Sign Out"), ), ] ], ), ), ); } }
Key Notes:
- Spreadsheet ID: Find this in your Google Sheet's URL—it's the long string between
/d/and/edit. - Permissions: Use
SheetsApi.spreadsheetsScopeif you need to edit the sheet, orspreadsheetsReadonlyScopefor read-only access. - OAuth Verification: If you're building a public app, you'll need to submit your OAuth consent screen for Google's verification to let non-test users sign in. Until then, only your added test users can access the app.
内容的提问来源于stack exchange,提问作者Jose M Vidal
相关产品推荐
相关产品推荐

