0

Google Cloud Storage 内のファイルを操作する Java アプリケーションを作成しています。gcloud-java私は仕事をしようとしているものを見つけました。

それらのを見ると、でログインした後に単純に実行できるはずgcloudですが、機能していないようです。を実行しようとしていますが、「指定されていない場合はログインしたプロジェクトが使用されますStorageExample」と表示されますが、ログインしても、プロジェクトを指定するかどうかに関係なくアクセスできません。

$ 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

明らかに私の環境に何か問題がありますが、それを修正するにはどうすればよいですか? 例を変更するのではなく、環境を修正したいと考えています。

ノート:

  • Windows 10のCygwin 2.0.4で実行しています
  • 私はMaven経由でビルドしていませんが、それが問題の原因ではないと思います
4

1 に答える 1

1

機能しない理由gcloud auth loginは、特に絶対パスに関して、Java が Cygwin とうまく連携しないためです。

gcloudは設定と資格情報を Cygwin ホーム ディレクトリに保存しますが、Java は を介し​​てWindows~/.config/gcloudホーム ディレクトリでそれらを探します。次のように、アプリケーションが環境変数を介してこのディレクトリを検索する場所を指定できます( を呼び出すときにのみ指定してください。シェルに設定すると、 break になります)。System.getProperty("user.home")CLOUDSDK_CONFIGjavagcloud

$ 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 ....

プロジェクトを指定する必要はありませんでしたが、何らかの理由でまだログインしていないことに注意してください。デモ アプリでは、認証エラーの報告がうまくいかないことがわかりました。AuthCredentials.createApplicationDefaults()メソッドの開始時にへの明示的な呼び出しを追加するmain()と、はるかに役立つエラー メッセージが報告されます。

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.

これで、少なくとも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=...}

成功!

于 2016-02-27T21:33:45.583 に答える