【发布时间】:2023-02-22 04:20:52
【问题描述】:
我不想在代码中硬编码我的 api 密钥和秘密,为此我想使用 google secret manager。为此,我正在使用 googleapis 包。 该软件包提供了一种访问 google secret manager 的方法。但我不知道如何使用它。如果有人能提供一个虚拟示例代码和流程,这将对我集成它有很大的帮助。 提前致谢
【问题讨论】:
-
你设法做到了吗?
标签: flutter dart google-api
我不想在代码中硬编码我的 api 密钥和秘密,为此我想使用 google secret manager。为此,我正在使用 googleapis 包。 该软件包提供了一种访问 google secret manager 的方法。但我不知道如何使用它。如果有人能提供一个虚拟示例代码和流程,这将对我集成它有很大的帮助。 提前致谢
【问题讨论】:
标签: flutter dart google-api
我就是这样做的。
首先,您必须从谷歌云下载一个服务帐户 json 并将其存储在您的资产文件夹中。
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:googleapis/secretmanager/v1.dart';
import 'package:googleapis_auth/auth_io.dart';
import 'package:http/http.dart' as http;
final String secretsPath = 'projects/$googleProjectId/secrets/$secretManagerName/versions/latest';
final credentials = json.decode(await rootBundle.loadString('assets/json/google-service-account.json'));
final AutoRefreshingAuthClient client = await clientViaServiceAccount(
ServiceAccountCredentials.fromJson(credentials), [SecretManagerApi.cloudPlatformScope],
baseClient: http.Client());
final SecretManagerApi api = SecretManagerApi(client);
Map<String, String> apiTokens = {};
final AccessSecretVersionResponse secrets = await api.projects.secrets.versions.access(secretsPath);
final String decoded = utf8.decode(base64Url.decode(secrets.payload!.data!));
// the secrets are stored in a file where each line is a secret
final List<String> items = decoded.split('
');
for (String line in items) {
final List<String> data = line.split('=');
if (data.length > 1) {
apiTokens[data[0]] = data[1];
}
}
【讨论】: