niaomingjian
niaomingjian

Reputation: 3742

Setting GOOGLE_APPLICATION_CREDENTIALS still doesn't enable the access to Google Storage

This answer shows how to access Google Cloud Storage with a service account in Java.
Using hard-coded Service Account Credentials, I could access GCS. However, using GOOGLE_APPLICATION_CREDENTIALS or running gcloud auth application-default login, I couldn't access GCS and got GoogleJsonResponseException: 401 Unauthorized.
My OS is Win7_64x.

Exception in thread "main" com.google.cloud.storage.StorageException: Anonymous caller does not have storage.objects.get access to nmjcloud_jar_test/addons/simple-bean-1.0.jar.
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:191)
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.get(HttpStorageRpc.java:342)
    at com.google.cloud.storage.StorageImpl$5.call(StorageImpl.java:198)
    at com.google.cloud.storage.StorageImpl$5.call(StorageImpl.java:195)
    at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:89)
    at com.google.cloud.RetryHelper.run(RetryHelper.java:74)
    at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:51)
    at com.google.cloud.storage.StorageImpl.get(StorageImpl.java:195)
    at com.google.cloud.storage.StorageImpl.get(StorageImpl.java:209)
    at com.example.storage.QuickstartSample.main(QuickstartSample.java:47)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
  "code" : 401,
  "errors" : [ {
    "domain" : "global",
    "location" : "Authorization",
    "locationType" : "header",
    "message" : "Anonymous caller does not have storage.objects.get access to nmjcloud_jar_test/addons/simple-bean-1.0.jar.",
    "reason" : "required"
  } ],
  "message" : "Anonymous caller does not have storage.objects.get access to nmjcloud_jar_test/addons/simple-bean-1.0.jar."
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.get(HttpStorageRpc.java:340)
    ... 8 more

How can I check where I was wrong?

Upvotes: 1

Views: 1542

Answers (1)

niaomingjian
niaomingjian

Reputation: 3742

Oh dear. I made a mistake.

I quoted up the variable value in the textbox of variable value.
Do not need to use quotation marks to enclose that string.

Incorrect: "/path/to/jsonfile.json"
Correct: /path/to/jsonfile.json

Upvotes: 1

Related Questions