How do we set GoogleCredentials for p12 file

Viewed 510

I need to use p12 file for GoogleCredentials instead of json file which was supported in the Deprecated class GoogleCredential (which was in our old code)

We have a solution to this using a deprecated version of GoogleCredential Use a .p12 File from classpath for GoogleCredential which just allowed me to use .setServiceAccountPrivateKeyFromP12File(new File(KEY_FILE_LOCATION))

GoogleCredentials class is now provided instead of GoogleCredential

So I have been trying this which gives an error

final GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(authFilePath))
        .createScoped(SERVICE_ACCOUNT_SCOPE)
        .newBuilder()
        .build();

I get this exception java.lang.IllegalArgumentException: expected numeric type but got class com.google.api.client.json.GenericJson

There is no documentation for p12 files

google credentials documentation shows this https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application

static void authExplicit(String jsonPath) throws IOException {
  // You can specify a credential file by providing a path to GoogleCredentials.
  // Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
  GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
        .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
  Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
  System.out.println("Buckets:");
  Page<Bucket> buckets = storage.list();
  for (Bucket bucket : buckets.iterateAll()) {
    System.out.println(bucket.toString());
  }
}

And from this page - https://cloud.google.com/iam/docs/creating-managing-service-account-keys#iam-service-account-keys-list-java

It reads - Note once again that the privateKeyData returned is a base64-encoded string representation of the TYPE_GOOGLE_CREDENTIALS_FILE value (JSON or P12 key/credentials). You can store the base64-decoded privateKeyData in a file with the appropriate extension (either .p12 or .json) and use it as input to client libraries or the gcloud auth activate-service-account command.

Any help is appreciated

Update :

My Final goal is to hit Google Hotel Apis https://developers.google.com/hotels , If there is a separate approach to this it should be good as well.

I use the access tokens from the credentials, to set the access token in headers in request factory

0 Answers
Related