使用 Google API
Google APIs package 提供了许多你可以从 Dart 项目中使用的 Google 服务。
本页面描述了如何通过 Google 身份验证,使用这些 API 和终端用户数据交互。
用户数据 API 的例子包括 Calendar、Gmail、YouTube 和 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.
概览
#请遵循以下步骤使用 Google API:
-
选择所需的 API
-
启用 API 服务
-
验证并确认当前用户
-
获取身份验证后的 HTTP 客户端
-
创建并使用所需的 API 类
1. 选择所需的 API
#文档 package:googleapis
采用 name_version
的形式,列举了每一个可以单独作为 Dart 库的 API。一起看看 youtube_v3
这个例子。
每个库都可能提供多种类型,但是一定会有一个以 Api
结尾的 根 类。在 YouTube 中,根类就是 YouTubeApi
。
Api
类不仅是你需要初始化的类(详见步骤 3),它还暴露了使用该 API 所需权限的作用域。例如, YouTubeApi
类中 常量 这一节,你会看到可用的作用域有哪些。为了获取终端用户的 YouTube 数据的读取(并非写入)权限,请使用 youtubeReadonlyScope
对用户进行验证。
/// Provides the `YouTubeApi` class.
import 'package:googleapis/youtube/v3.dart';
2. 启用 API 服务
#使用 Google API,你必须有一个 Google 账户和一个 Google 项目。你还需要启用所需的 API 服务。
在本示例中,你将需要启用 YouTube Data API v3 服务。
详情请看 入门指南。
3. 验证并确认当前用户
#使用 google_sign_in package 对用户进行 Google 身份验证。为你需要的平台配置登录。
/// Provides the `GoogleSignIn` class.
import 'package:google_sign_in/google_sign_in.dart';
该 package 的功能是通过 GoogleSignIn
类的静态实例访问的。在于该实例交互之前,必须调用 initialize
方法并等待其执行完成。
final _googleSignIn = GoogleSignIn.instance;
@override
void initState() {
super.initState();
_googleSignIn.initialize();
// ···
}
初始化完成后,在用户身份验证之前监听身份验证事件,以确认用户是否已登录。
GoogleSignInAccount? _currentUser;
@override
void initState() {
super.initState();
_googleSignIn.initialize().then((_) {
_googleSignIn.authenticationEvents.listen((event) {
setState(() {
_currentUser = switch (event) {
GoogleSignInAuthenticationEventSignIn() => event.user,
_ => null,
};
});
});
});
}
在监听任何相关的身份验证事件后,你就可以尝试为之前登录过的用户进行身份验证。
void initState() {
super.initState();
_googleSignIn.initialize().then((_) {
// ...
// Attempt to authenticate a previously signed in user.
_googleSignIn.attemptLightweightAuthentication();
});
}
为了允许新用户进行身份验证,请遵循 package:google_sign_in
提供的说明。
用户身份验证通过后,你必须获取一个验证后的 HTTP 客户端。
4. 获取身份验证后的 HTTP 客户端
#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,
);
Client
实例包含了调用 Google API 类时所需的凭证。
5. 创建并使用所需的 API 类
#使用 API 来创建所需的 API 类型和调用方法,例如:
final youTubeApi = YouTubeApi(authenticatedClient);
final favorites = await youTubeApi.playlistItems.list(
['snippet'],
playlistId: 'LL', // Liked List
);
更多信息
#你可能还需要了解以下内容:
-
示例
extension_google_sign_in_as_googleapis_auth
是本页面所述概念的一个可行的实现。
除非另有说明,本文档之所提及适用于 Flutter 的最新稳定版本,本页面最后更新时间: 2025-09-02。 查看文档源码 或者 为本页面内容提出建议.