Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How can I explicitly authenticate to the ai-platform using the java PredictionServiceClient

I have a model hosted on a Google Cloud endpoint and I would like to access it via the Java client.  I've created a service account and a key for that service account with the , when I run my client code with the GOOGLE_APPLICATION_CREDENTIALS env var pointed to the key, I am able to call the service.  When I try to authenticate explicitly using FixedCredentialProvider, it fails with an "unauthenticated" message.  

The code is as follows

```

PredictionServiceSettings predictionServiceSettings =
PredictionServiceSettings.newBuilder().setEndpoint(location + "-aiplatform.googleapis.com:443")
.setCredentialsProvider(FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream("/Users/ME/Downloads/XYZ.json"))))
.build();
predictionServiceClient = PredictionServiceClient.create(predictionServiceSettings);
endpointName = EndpointName.of(project, location, endpointId);
Value featureVal = Value.newBuilder().setStructValue(features).build();
PredictResponse response = predictionServiceClient.predict(
endpointName,
Collections.singletonList(featureVal),
Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());

```

 

Solved Solved
0 2 2,559
1 ACCEPTED SOLUTION

Hi,

Upon checking your code, FixedCredentialsProvider.create() accepts com.google.auth.Credentials as a parameter. Can you try a Credentials object to FixedCredentialsProvider.create()? See code below:

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("/Users/ME/Downloads/XYZ.json")).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));

 If code above did not work, can you provide the stack trace of the error? Also what roles did you assign on your service account?

View solution in original post

2 REPLIES 2

Hi,

Upon checking your code, FixedCredentialsProvider.create() accepts com.google.auth.Credentials as a parameter. Can you try a Credentials object to FixedCredentialsProvider.create()? See code below:

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("/Users/ME/Downloads/XYZ.json")).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));

 If code above did not work, can you provide the stack trace of the error? Also what roles did you assign on your service account?

This worked. Thank you!