Search code examples
flutterdartgoogle-cloud-platformgoogle-secret-manager

How to use Google Secret Manager with Flutter


I am trying to use https://pub.dev/packages/googleapis and get a value from the SecretManagerApi.

This is my current code but it doesn't even resolve.

final secretVersion = await SecretManagerApi(Client())
          .projects
          .secrets
          .get("projects/myProjectId/secrets/mySec/versions/latest")

so secretVersion never gets a value. What am I doing wrong?


Solution

  • This is one way I use to authenticate with Secret Manager:

    Add your google json file to path and then export GOOGLE_APPLICATION_CREDENTIALS=<YOUR_PATH_TO_JSON_FILE>

    Then change to this code:

    import 'package:googleapis/secretmanager/v1.dart';
    ...
    final client await clientViaApplicationDefaultCredentials(
        scopes: [FirestoreApi.cloudPlatformScope]);
    final api = SecretManagerApi(client);
    final secrets = await api.projects.secrets.versions.access(<YOUR_SECRET_NAME>);
    print(secrets.payload!.data.toString());
    // print(utf8.decode(secrets.payload!.dataAsBytes));
    ...