Flutter Google API客户端插件googleapis_client的使用
Flutter Google API客户端插件googleapis_client的使用
库 googleapis_client
被创建是为了简化使用 Google APIs 的过程,尤其是对于新手或初学者来说。官方库可能较为复杂。
创建Secret API Key
- Service Account
- 打开你的浏览器并访问 https://console.cloud.google.com
- 创建一个项目或选择一个现有的项目
- 打开“API 与服务”选项卡
- 点击“创建凭据”
- 选择“服务账号”
详细步骤请参见以下视频:
安装库
在 pubspec.yaml
文件中添加以下依赖:
dart pub add googleapis_client
添加库
在 Dart 文件中导入库:
import 'package:googleapis_client/googleapis_client.dart';
文档
如果你没有足够的网络流量,可以使用以下命令下载文档:
git clone https://github.com/azkadev/googleapis_client.git
cd googleapis_client
cd web
flutter clean
flutter pub get
flutter run
快速开始
以下是一个简单的示例,展示如何使用 googleapis_client
库来调用 YouTube 和 Gmail API:
import 'dart:convert';
import 'package:googleapis_client/googleapis_client.dart';
void prettyPrint(dynamic data) {
if (data is Map || data is List) {
print(JsonEncoder.withIndent(" ").convert(data));
} else {
print(data.toString());
}
}
void main(List<String> args) async {
GoogleApisClient googleApisClient = GoogleApisClient(
googleApisClientApiKey: GoogleApisClientApiKey(
{
"type": "service_account",
"project_id": "nod",
"private_key_id": "",
"private_key": "-----BEGIN -----END PRIVATE KEY-----\n",
"client_email": "mkkm",
"client_id": "1580",
"auth_uri": "https://o/oauth2/auth",
"token_uri": "https:/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "",
"universe_domain": "",
},
),
);
await googleApisClient.youtube.subscribeChannel(channel: "@azkadev");
await googleApisClient.youtube.addComment(videoId: "HV4kn5j7IwQ", text: "Hai ini pesan automatis");
// 使用 Dart 类模式
await googleApisClient.request(
requestData: YoutubeGetChannel.create(
special_type: "youtubeGetChannel",
channel_id: "@azkadev",
),
);
// 使用 Dart 类模式
await googleApisClient.request(
requestData: JsonApis({
"@type": "youtubeSubscribeChannel",
"@client_channel": "",
"channel_id": "@azkadev",
}),
);
await googleApisClient.request(
requestData: JsonApis({
"@type": "gmailSendMessage",
"email_id": "email@gmail.com",
"text": "",
}),
);
}
示例客户端使用此库
以下是一个完整的示例,展示了如何在 Flutter 应用程序中使用 googleapis_client
库:
import 'package:flutter/material.dart';
import 'package:googleapis_client/googleapis_client.dart';
void main() {
GoogleApisClient googleApisClient = GoogleApisClient(googleApisClientApiKey: GoogleApisClientApiKey({}));
googleApisClient;
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('You have pushed the button this many times:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
更多关于Flutter Google API客户端插件googleapis_client的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复