initViaServiceAccountJson static method

Future<void> initViaServiceAccountJson(
  1. String serviceAccountJson, {
  2. List<String>? scopes,
  3. Client? httpClient,
})

This method initializes the GoogleSecretManager instance via the given service account JSON credentials.

https://developers.google.com/workspace/guides/create-credentials#service-account

Implementation

static Future<void> initViaServiceAccountJson(
  String serviceAccountJson, {
  List<String>? scopes,
  http.Client? httpClient,
}) async {
  final jsonData = jsonDecode(serviceAccountJson);
  final projectId = jsonData['project_id'];
  if (projectId == null) {
    throw Exception('No project_id present in the service account json');
  }

  final credentials = ServiceAccountCredentials.fromJson(jsonData);

  final client = await clientViaServiceAccount(
    credentials,
    [
      if (scopes != null && scopes.isNotEmpty)
        ...scopes
      else
        SecretManagerApi.cloudPlatformScope,
    ],
    baseClient: httpClient ?? http.Client(),
  );
  GoogleSecretManager.instance = _GoogleSecretManagerImpl._(
    SecretManagerApi(client),
    projectId: projectId,
  );
}