Which part of the private key for service accounts

2020-02-15 06:28发布

问题:

I am trying to verify I have the private_key correct as described in this SO question.

Docs about service account private keys are here.

{
"type": "service_account",
"project_id": "[PROJECT-ID]",
"private_key_id": "[KEY-ID]",
"private_key": "-----BEGIN PRIVATE KEY-----\n[PRIVATE-KEY]\n-----END PRIVATE KEY-----\n",
"client_email": "[SERVICE-ACCOUNT-EMAIL]",
"client_id": "[CLIENT-ID]",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/[SERVICE-ACCOUNT-EMAIL]"
}

When using private_key, do I just get the part in between the

-----BEGIN PRIVATE KEY-----\n

string and the

\n-----END PRIVATE KEY-----\n

In other words, the [PRIVATE-KEY] shown in the example?

Or do I get the entire string including the two "bookend" strings shown above?

Also, within the [PRIVATE-KEY] string, there are several occurrences of the \n substring. Do I remove them or leave them in?

回答1:

This question is also related to another question, so look out for similar symptoms when debugging.

Troubleshooting steps:

  1. Verify that the JSON file is the one generated for your service acocunt
  2. Verify that the JSON file is being read properly.
  3. Verify that you are using the correct Firebase methods

Conclusion:

This issue with Firebase authentication was caused by a malfunctioning JSON reading.

It's important to note that the private keys are meant to be used with the "useless" information, since the method to decode the private key is expecting those strings to be there.

TL;DR

The problem was reading the JSON file correctly.

Thanks to @Tanaike and @Tedinoz for the troubleshooting steps.