Google APIs
The Google APIs package exposes dozens of Google services that you can use from Dart projects.
This page describes how to use APIs that interact with end-user data by using Google authentication.
Examples of user-data APIs include Calendar, Gmail, YouTube, and Firebase.
To add authentication to Firebase explicitly, check out the Add a user authentication flow to a Flutter app using FirebaseUI codelab and the Get Started with Firebase Authentication on Flutter docs.
Overview
#To use Google APIs, follow these steps:
- Pick the desired API
- Enable the API
- Authenticate and determine the current user
- Obtain an authenticated HTTP client
- Create and use the desired API class
1. Pick the desired API
#The documentation for package:googleapis
lists each API as a separate Dart library&emdash;in a name_version
format. Check out youtube_v3
as an example.
Each library might provide many types, but there is one root class that ends in Api
. For YouTube, it's YouTubeApi
.
Not only is the Api
class the one you need to instantiate (see step 3), but it also exposes the scopes that represent the permissions needed to use the API. For example, the Constants section of the YouTubeApi
class lists the available scopes. To request access to read (but not write) an end-user's YouTube data, authenticate the user with youtubeReadonlyScope
.
/// Provides the `YouTubeApi` class.
import 'package:googleapis/youtube/v3.dart';
2. Enable the API
#To use Google APIs you must have a Google account and a Google project. You also need to enable your desired API.
This example enables YouTube Data API v3. For details, see the getting started instructions.
3. Authenticate and determine the current user
#Use the google_sign_in package to authenticate users with their Google identity. Configure sign in for each platform you want to support.
/// Provides the `GoogleSignIn` class.
import 'package:google_sign_in/google_sign_in.dart';
The package's functionality is accessed through a static instance of the GoogleSignIn
class. Before interacting with the instance, the initialize
method must be called and allowed to complete.
final _googleSignIn = GoogleSignIn.instance;
@override
void initState() {
super.initState();
_googleSignIn.initialize();
// ยทยทยท
}
Once initialization is complete but before user authentication, listen to authentication events to determine if a user signed in.
GoogleSignInAccount? _currentUser;
@override
void initState() {
super.initState();
_googleSignIn.initialize().then((_) {
_googleSignIn.authenticationEvents.listen((event) {
setState(() {
_currentUser = switch (event) {
GoogleSignInAuthenticationEventSignIn() => event.user,
_ => null,
};
});
});
});
}
Once you're listening to any relevant authentication events, you can attempt to authenticate a previously signed-in user.
void initState() {
super.initState();
_googleSignIn.initialize().then((_) {
// ...
// Attempt to authenticate a previously signed in user.
_googleSignIn.attemptLightweightAuthentication();
});
}
To also allow for new users to authenticate, follow the instructions provided by package:google_sign_in
.
Once a user has been authenticated, you must obtain an authenticated HTTP client.
4. Obtain an authenticated HTTP client
#Once you have a signed-in user, request the relevant client authorization tokens using authorizationForScopes
for the API scopes that your app requires.
const relevantScopes = [YouTubeApi.youtubeReadonlyScope];
final authorization = await currentUser.authorizationClient
.authorizationForScopes(relevantScopes);
Once you have the relevant authorization tokens, use the authClient
extension from package:extension_google_sign_in_as_googleapis_auth
to set up an authenticated HTTP client with the relevant credentials applied.
import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';
final authenticatedClient = authorization!.authClient(
scopes: relevantScopes,
);
5. Create and use the desired API class
#Use the API to create the desired API type and call methods. For instance:
final youTubeApi = YouTubeApi(authenticatedClient);
final favorites = await youTubeApi.playlistItems.list(
['snippet'],
playlistId: 'LL', // Liked List
);
More information
#You might want to check out the following:
- The
extension_google_sign_in_as_googleapis_auth
example is a working implementation of the concepts described on this page.
Unless stated otherwise, the documentation on this site reflects the latest stable version of Flutter. Page last updated on 2025-08-21. View source or report an issue.