niaomingjian
niaomingjian

Reputation: 3742

com.google.cloud.storage.StorageException: 401 Unauthorized (from local machine)

GoogleCloudPlatform/storage/cloud-client sample could run on GCE VM.
But when running on my local machine(Win7_64x), I got StorageException(401 Unauthorized).
I set up authentication by this doc.

Could the program access google storage from my local machine?
What else shoud I do to achieve this? or there isn't a way to access google storage outside of GCP?

Exception in thread "main" com.google.cloud.storage.StorageException: 401 Unauthorized
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:191)
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.create(HttpStorageRpc.java:221)
    at com.google.cloud.storage.StorageImpl$2.call(StorageImpl.java:112)
    at com.google.cloud.storage.StorageImpl$2.call(StorageImpl.java:109)
    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.create(StorageImpl.java:108)
    at com.example.storage.QuickstartSample.main(QuickstartSample.java:35)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
    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.create(HttpStorageRpc.java:219)
    ... 7 more

UPDATE(20180309):
Running the following code with hard-coded Service Account Credentials, I could access google storage and download files from it.

String SERVICE_ACCOUNT_JSON_PATH = "C:\\gcpconfig\\My First Project-6f9cff47c4f0.json";

Storage storage =
    StorageOptions.newBuilder()
        .setCredentials(
            ServiceAccountCredentials.fromStream(
                new FileInputStream(SERVICE_ACCOUNT_JSON_PATH)))
        .build()
        .getService();

BlobId blobId = BlobId.of("nmjcloud_jar_test","addons/simple-bean-1.0.jar");
Blob blob = storage.get(blobId);

Path path = Paths.get("D:\\lib\\simple-bean-1.0.jar");
blob.downloadTo(path);

System.out.printf("Download successfully%n");

Unfortunately, Setting GOOGLE_APPLICATION_CREDENTIALS environment variable to the full path of my key json file still doesn't work at all. It seems like that the google storage api library doesn't read key json file from GOOGLE_APPLICATION_CREDENTIALS.

Oh dear. It's My Mistake

Upvotes: 1

Views: 4193

Answers (1)

Mar Cial R
Mar Cial R

Reputation: 946

As is mentioned on java-docs-samples, you need to define some kind of valid credentials for your code to work properly. In your local machine the two valid options are:

  1. Set GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to a service account key JSON file path (Make sure to not surround it with double/single quotes)
  2. Execute gcloud auth application-default login

Given the doc you've mentioned has similar instructions, I'd suggest you to try option 2 while at the same time, check for previous mispellings, permissions of the object you are trying to access...

Note: here you'll find how to create the JSON key file and here how to set the environment variable.

Upvotes: 3

Related Questions