如何使用Google云端存储验证我的Java应用程序? [英] How do I authenticate my Java application with Google Cloud Storage?

查看:1759
本文介绍了如何使用Google云端存储验证我的Java应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Java应用程序来与Google云端存储中的文件进行交互。我找到 gcloud-java



查看他们的 examples 似乎我应该能够在登录 gcloud 后运行它们,但它不会似乎工作。我正在尝试运行 StorageExample ,其中显示如果未提供则将使用已登录的项目,但尽管已登录我无法访问

  $ gcloud auth login 
您的浏览器已启用浏览器工具:

https://accounts.google.com/o/oauth2/auth?....


保存的应用程序默认凭据。

您现在以[...]身份登录。
您当前的项目是[...]。您可以通过运行以下命令来更改此设置:
$ gcloud config set project PROJECT_ID

$ java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list
Exception in threadmainjava.lang.IllegalArgumentException:此服务需要项目ID,但无法从构建器或环境中确定。请使用构建器设置项目ID。
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)
at com.google.gcloud.ServiceOptions。< init>(ServiceOptions.java:317)
...
at com.google.gcloud.examples.storage.StorageExample.main(StorageExample.java:566)

$ java -cp GCloudDemo.jar com.google.gcloud。 examples.storage.StorageExample ... list
线程main中的异常com.google.gcloud.storage.StorageException:需要登录com.google.gcloud.spi.DefaultStorageRpc.translate上的
(DefaultStorageRpc。 java:94)
在com.google.gcloud.spi.DefaultStorageRpc.list(DefaultStorageRpc.java:148)
...
在com.google.gcloud.examples.storage.StorageExample $ ListAction.run(StorageExample.java:1)
在com.google.gcloud.examples.storage.StorageExample.main(StorageExample.java:579)
引发者:com.google.api.client .googleapis.json.GoogleJsonResponseException:401未授权
{
code:401,
errors:[{
domain:global,
location:Authorization,
locationType:header,
message:需要登录,
reason:必填
} ,
message:需要登录
}
在com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
。 ..
at com.google.gcloud.spi.DefaultStorageRpc.list(DefaultStorageRpc.java:138)
...另外10个

显然,我的环境有些问题,但是我需要做什么来解决它呢?



注意:




  • 我在Windows 10上运行Cygwin 2.0.4

  • 我不是通过Maven构建的,但我假设

    原因 gcloud auth login / code>不工作是因为Java不能很好地与Cygwin,特别是关于绝对路径。



    gcloud 将您的设置和凭据存储在Cygwin主目录〜/ .config / gcloud 中,但Java将在您的 > Windows 主目录(通过 System.getProperty(user.home) 。您可以通过 CLOUDSDK_CONFIG 环境变量指定应用程序应该查找此目录的位置,如下所示(确保只有在调用 java ;设置它为你的shell将反过来打破 gcloud ):

     code> $ CLOUDSDK_CONFIG = $(cygpath -w〜/ .config / gcloud)\ 
    java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list
    线程maincom.google.gcloud.storage.StorageException:需要登录
    at ....


    $ b b

    请注意,我们不必指定项目,但我们仍然没有因为某种原因登录。事实证明,演示应用程序没有做很好的报告身份验证错误的工作。在 main()方法开头添加对 AuthCredentials.createApplicationDefaults()的显式调用报告更有用错误消息:


     线程main中的异常java.io.IOException:Application Default Credentials是
    不可用。如果在Google Compute Engine中运行,则可以使用它们。否则,必须定义
    环境变量GOOGLE_APPLICATION_CREDENTIALS,指向定义凭据的
    文件。有关详情,请参阅https://developers.google.com/accounts/docs/application-default-credentials


    现在我们至少有 GOOGLE_APPLICATION_CREDENTIALS 环境变量。但是为什么 gcloud 为我们配置东西呢?它看起来像是图书馆中的错误; 现在可以找到我们的登录令牌。



    同时,我们也可以通过明确指定凭据路径解决它:

      $ CLOUDSDK_CONFIG = $(cygpath -w〜/ .config / gcloud)\ 
    GOOGLE_APPLICATION_CREDENTIALS = $(cygpath -w〜/ .config / gcloud)/application_default_credentials.json \
    java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list
    Bucket {name = ...}

    成功!


    I am writing a Java application to interact with files in Google Cloud Storage. I found gcloud-java which I'm trying to get working.

    Looking at their examples it seems I should be able to simply run them after having logged-in with gcloud, but it doesn't seem to be working. I'm trying to run StorageExample, which says "logged-in project will be used if not supplied", but despite logging in I cannot access my project whether I specify it or not.

    $ gcloud auth login
    Your browser has been opened to visit:
    
        https://accounts.google.com/o/oauth2/auth?....
    
    
    Saved Application Default Credentials.
    
    You are now logged in as [...].
    Your current project is [...].  You can change this setting by running:
      $ gcloud config set project PROJECT_ID
    
    $ java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list
    Exception in thread "main" java.lang.IllegalArgumentException: A project ID is required for this service but could not be determined from the builder or the environment.  Please set a project ID using the builder.
            at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)
            at com.google.gcloud.ServiceOptions.<init>(ServiceOptions.java:317)
            ...
            at com.google.gcloud.examples.storage.StorageExample.main(StorageExample.java:566)
    
    $ java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample ... list
    Exception in thread "main" com.google.gcloud.storage.StorageException: Login Required
            at com.google.gcloud.spi.DefaultStorageRpc.translate(DefaultStorageRpc.java:94)
            at com.google.gcloud.spi.DefaultStorageRpc.list(DefaultStorageRpc.java:148)
            ...
            at com.google.gcloud.examples.storage.StorageExample$ListAction.run(StorageExample.java:1)
            at com.google.gcloud.examples.storage.StorageExample.main(StorageExample.java:579)
    Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
    {
      "code" : 401,
      "errors" : [ {
        "domain" : "global",
        "location" : "Authorization",
        "locationType" : "header",
        "message" : "Login Required",
        "reason" : "required"
      } ],
      "message" : "Login Required"
    }
            at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
            ...
            at com.google.gcloud.spi.DefaultStorageRpc.list(DefaultStorageRpc.java:138)
            ... 10 more
    

    Clearly something is wrong with my environment, but what do I have to do to fix it? I'd like to correct my environment rather than make changes to the example.

    Notes:

    • I'm running in Cygwin 2.0.4 on Windows 10
    • I'm not building via Maven, but I assume that isn't the source of the problem

    解决方案

    The reason gcloud auth login isn't working is because Java doesn't play well with Cygwin, specifically with regards to absolute paths.

    gcloud stores your settings and credentials in your Cygwin home directory, ~/.config/gcloud, but Java is going to look for them in your Windows home directory via System.getProperty("user.home"). You can specify where the application should look for this directory via the CLOUDSDK_CONFIG environment variable, like so (be sure to only specify it when calling java; setting it for your shell will in turn break gcloud):

    $ CLOUDSDK_CONFIG=$(cygpath -w ~/.config/gcloud) \
      java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list 
    Exception in thread "main" com.google.gcloud.storage.StorageException: Login Required
        at ....
    

    Notice that that we didn't have to specify a project, but we're still not logged in for some reason. It turns out the demo app doesn't do a great job of reporting authentication errors. Adding an explicit call to AuthCredentials.createApplicationDefaults() at the start of the main() method reports a much more helpful error message:

    Exception in thread "main" java.io.IOException: The Application Default Credentials are
    not available. They are available if running in Google Compute Engine. Otherwise, the
    environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a
    file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials
    for more information.
    

    So now we at least have the GOOGLE_APPLICATION_CREDENTIALS environment variable to go on. But why isn't gcloud configuring things for us? It looks like that's actually a bug in the library; it should now be able to find our login token.

    In the meantime, we can workaround it by explicitly specifying the credentials path as well:

    $ CLOUDSDK_CONFIG=$(cygpath -w ~/.config/gcloud) \
      GOOGLE_APPLICATION_CREDENTIALS=$(cygpath -w ~/.config/gcloud)/application_default_credentials.json \
      java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list
    Bucket{name=...}
    

    Success!

    这篇关于如何使用Google云端存储验证我的Java应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆