title: com.google.cloud.storage.StorageException: 401 Unauthorized (from local machine)
body: <p><a href="https://github.com/GoogleCloudPlatform/java-docs-samples/tree/8a50ab6a8dd220d87b4b259021e53f7f429ca758/storage/cloud-client" rel="nofollow noreferrer">GoogleCloudPlatform/storage/cloud-client sample</a> could run on GCE VM.<br>
  But when running on my local machine(Win7_64x), I got StorageException(401 Unauthorized).<br>
  I set up authentication by <a href="https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-java" rel="nofollow noreferrer">this doc</a>.  </p>
  
  <p>Could the program access google storage from my local machine?<br>
  What else shoud I do to achieve this? or there isn't a way to access google storage outside of GCP?  </p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>UPDATE(20180309)</strong>:<br>
  Running the following code with <strong><a href="https://stackoverflow.com/a/45121635/4983501">hard-coded Service Account Credentials</a></strong>, I could access google storage and download files from it.</p>
  
  <pre><code>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");
  </code></pre>
  
  <p>Unfortunately, Setting <strong>GOOGLE_APPLICATION_CREDENTIALS</strong> environment variable to the full path of my key json file <strong>still doesn't work</strong> at all. It seems like that the google storage api library doesn't read key json file from <code>GOOGLE_APPLICATION_CREDENTIALS</code>.</p>
  
  <p><strong><a href="https://stackoverflow.com/a/49188658/4983501">Oh dear. It's My Mistake</a></strong></p>