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! Go to Solution.
Hi,
Upon checking your code, FixedCredentialsProvider.create() accepts com.
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?