Get Firebase Admin SDK private key programmatically

Viewed 514

I need to create Firebase projects, get the Admin SDK private keys, and add resources programmatically.

I can create projects with CLI - firebase projects:create. But after that to use Admin SDK or REST API to add resources to the project, I need to get Admin SDK private key.

My plan is web scraping with direct HTTP requests. I know Fastlane use same logic to control Apple Developer Portal UI and fetch data from it programmatically.

First, I need to go: https://console.firebase.google.com/project/<project_id>/settings/serviceaccounts/adminsdk with required cookies, headers etc.

Then, I should mimic the Generate new Private Key button below and get the private key.

But I have never used this method, especially with a web page that contaions sessions, cookies etc. And, I want to build it as a microservice and deploy to a server. So, I believe 2FA might be a problem.

My questions:

  • Is there any more cost-effective way to get Admin SDK? Maybe, I am missing some feature in Firebase resources.
  • Can I mimic this button's behaviour without using any front-end scraping? Is there any precautions in Firebase servers to prevent this kind of solution.
  • If I do it in this way and deploy to a server, would 2FA be a problem? Can microservice work with 1-month valid sessions? Or, would it asks 2FA all the time that it works? (I have never used headless browsers etc. I just some basic web-scraping experience with Selenium.)

Generate new private key button

1 Answers

It looks like scraping is not necessary, gcloud CLI do the job. If you want to create Firebase projects and get service accounts programmatically do the steps below:

  • Create with firebase CLI: firebase projects:create --display-name "<project-name>" <project-id> (Or, probably you can create with gcloud CLI as well)
  • Change active project in gcloud CLI with gcloud config set project <project-id>
  • gcloud iam service-accounts list and store the EMAIL here. (There is just automatically created service account here, no private key created.)
  • Create and store the private key: gcloud iam service-accounts keys create key.json --iam-account= <EMAIL-you-stored>
Related