Search code examples
firebaseservice-accountsfirebase-toolsfirebase-app-distribution

Firebase app distribution, failing to authenticate with service account


I am using firebase app distribution via firebase CLI in bitbucket pipeline to deploy app to the firebase. As long as i started to get errors:

HTTP Error: 401, Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.

And FIREBASE_TOKEN method will be deprecated and removed soon i decided to use GOOGLE_APPLICATION_CREDENTIAL for authentication.

Unfortunately i cannot authenticate with GOOGLE_APPLICATION_CREDENTIALS,

- firebase appdistribution:distribute 

request just fails with error, just ignoring GAC at all.

Error: Failed to authenticate, have you run firebase login?

Reading firebase docs and troubleshooting for last three days but cant get it working.

  • Google service account created, with Firebase App Distrib. Admin role.
  • JSON Key generated.
  • JSON key contents is directly declared in private GOOGLE_APPLICATION_CREDENTIALS variable. If i echo GOOGLE_APPLICATION_CREDENTIALS variable public i am getting exact JSON key contents just as i would get it from JSON file, so it should work in theory (I am trying to
{
  "type": "service_account",
  "project_id": "xxxx",
  "private_key_id": "xxx, ",
  "private_key": "-----BEGIN PRIVATE KEY-----\nxxxxx\n-----END PRIVATE KEY-----\n",
  "client_email": "xxxx",
  "client_id": "xxxx",
  "auth_uri": "xxxx",
  "token_uri": "xxxx",
  "auth_provider_x509_cert_url": "xxxx",
  "client_x509_cert_url": "xxxx",
  "universe_domain": "xxxx"
}

But firebase CLI does not use it for auth. anyway.


Solution

  • I found solution. GOOGLE_APPLICATION_CREDENTIALS must be the path to the .json file, not the JSON key content itself.

    So, you need to store your key as file anyway, place JSON key contents in private variable (char escape not required), then in script you can echo it to .json file and then you can use path to this file in your GOOGLE_APPLICATION_CREDENTIALS variable.

    echo $GAC_KEY > service-account.json
    export GOOGLE_APPLICATION_CREDENTIALS="/folder/service-account.json"