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, and YouTube.
Overview
To use Google APIs, follow these steps.
- Pick the desired API
- Enable the API
- Authenticate user with the required scopes
- 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 – in a name_version
format. Let’s look at
youtube_v3
as an example.
Each library may 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.
Look under the
Constants section
of the YouTubeApi
class and you’ll see the available scopes. To request access
to simply read (but not write) an end-users YouTube data, use the
youtubeReadonlyScope
when authenticating the user.
/// 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.
In this example, you’d enable YouTube Data API v3.
For details, see the getting started instructions.
3. Authenticate the user with the required scopes
Use the google_sign_in package to authenticate users with their Google identity. You will have to configure signin for each platform you want to support.
/// Provides the `GoogleSignIn` class
import 'package:google_sign_in/google_sign_in.dart';
When you instantiate the
GoogleSignIn
class, you provide the desired scopes as discussed in the previous section.
final _googleSignIn = GoogleSignIn(
scopes: <String>[YouTubeApi.youtubeReadonlyScope],
);
Follow the instructions provided by package:google_sign_in to allow a user to authenticate.
Once authenticated, you must obtain an authenticated HTTP client.
4. Obtain an authenticated HTTP client
The
extension_google_sign_in_as_googleapis_auth
package provides an
extension method on
GoogleSignIn
:
authenticatedClient
.
import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';
You can listen to
onCurrentUserChanged
.
When event value is not null
, you can create an authenticated client.
var httpClient = (await _googleSignIn.authenticatedClient())!;
This Client
instance
includes the nessesary credentials when invoking Google API classes.
5. Create and use the desired API class
Use the API to create the desired API type and call methods, for instance:
var youTubeApi = YouTubeApi(httpClient);
var favorites = await youTubeApi.playlistItems.list(
['snippet'],
playlistId: 'LL', // Liked List
);
More information
- The
extension_google_sign_in_as_googleapis_auth
example is a working implementation of the concepts described on this page.