Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Service account Json key file

Google has recently started to give us a Json key file for service account instead of the P12 key file. I have been trying to get this there isn't a lot of information out there and what info I have seen says this should work.

string[] scopes = new string[] { DriveService.Scope.Drive}; 

Stream stream = new FileStream(jsonKeyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
var credential = GoogleCredential.FromStream(stream).CreateScoped(scopes); 

However its throwing the following exception

Error creating credential from JSON. Unrecognized credential type .

I have double checked the json key file downloaded two different ones trying to get it to work nothing.

like image 572
DaImTo Avatar asked Mar 11 '16 08:03

DaImTo


2 Answers

I think you are using file google-services.json downloaded from Firebase. This isn't file you need. Process you have to do is:

  1. Go to Firebase Console
  2. Click on Settings
  3. Click on Project Settings
  4. Click on Service accounts
  5. Click on Generate new private key button
  6. Use file generated by this method.
like image 189
Kamil Avatar answered Sep 23 '22 13:09

Kamil


I downloaded a Service Account json key file and tried the same code and it works fine. I was able to replicate the issue if I manually corrupted the "type" field in the json key file. Can you re-download a json key file and try again. Here is what the json key file should look like (sensitive data removed):

{
  "type": "service_account",
  "project_id": ...,
  "private_key_id": ...,
  "private_key": ...,
  "client_email": ...,
  "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": ...
}
like image 24
Vijay Subramani Avatar answered Sep 22 '22 13:09

Vijay Subramani