Reputation: 785
I am just starting off on google-cloud-platform. I have created an account and a project on cloud console. I was trying to run some of the sample apps provided. I started with the sample app for cloud storage provided at:
https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/storage/cloud-client
I have installed apache maven 3.5.0 on my PC. I followed the steps provided in the link, that is I gave the following commands:
mvn clean package -DskipTests
and then
mvn exec:java -Dexec.mainClass=com.example.storage.QuickstartSample -Dexec.args="my-bucket-name"
The first command succeeded. However, the second command failed. I got the following error:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:06 min
[INFO] Finished at: 2017-06-15T18:27:55+05:30
[INFO] Final Memory: 15M/172M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project storage-google-cloud-samples: An exception occured while executing the Java class. connect timed out -> [Help 1]
Now, the computer where I was running this command on, is behind a proxy. However, my proxy settings have been set in the file conf/settings.xml, also when I ran the first command, it successfully downloaded some packages, so I'm not sure if it is due to some proxy issue, however to check, I tried it on another machine, which is not behind a proxy.
I gave the same two commands. The first one succeeded and the second failed again, with the following (different) error:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:02 min
[INFO] Finished at: 2017-06-15T18:22:31+05:30
[INFO] Final Memory: 13M/32M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (d
efault-cli) on project storage-google-cloud-samples: An exception occured while
executing the Java class. 401 Unauthorized -> [Help 1]
So my queries are:
What could be the cause for the error in the first case? Is it something to do with proxy settings? If yes, then where/how should I specify the settings?
What could be the cause for the error in the second case where I am not behind any proxy?
Am i missing some step here?
Also , if you look at the source for this sample app, there is just a single file which basically creates a bucket. The bucket is passed as an argument from command line. Now, as per my understanding, first there needs to a project in the cloud console to create any resources. So where will this bucket be created? As in shouldn't we be specifying the project-ID where this bucket is to be created?
Upvotes: 0
Views: 1225
Reputation: 49583
TL;DR - You're missing the credentials, the example relies on to invoke the desired Google Cloud APIs. Using Application Default Credentials
is the recommended approach when using the Google Cloud APIs using any of the Client libraries.
The example relies on Application Default Credentials
(as explained in the README.md
of the github repo you're using).
How the Application Default Credentials work
You can get Application Default Credentials by making a single client library call. The credentials returned are determined by the environment the code is running in. Conditions are checked in the following order:
The environment variable
GOOGLE_APPLICATION_CREDENTIALS
is checked. If this variable is specified it should point to a file that defines the credentials. The simplest way to get a credential for this purpose is to create a Service account key in the Google API Console:a. Go to the API Console Credentials page.
b. From the project drop-down, select your project.
c. On the Credentials page, select the Create credentials drop-down, then select Service account key.
d.From the Service account drop-down, select an existing service account or create a new one.
e. For Key type, select the JSON key option, then select Create. The file automatically downloads to your computer.
f. Put the *.json file you just downloaded in a directory of your choosing. This directory must be private (you can't let anyone get access to this), but accessible to your web server code.
g. Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file downloaded.If you have installed the Google Cloud SDK on your machine and have run the command
gcloud auth application-default login
, your identity can be used as a proxy to test code calling APIs from that machine.If you are running in Google App Engine production, the built-in service account associated with the application will be used.
If you are running in Google Compute Engine production, the built-in service account associated with the virtual machine instance will be used.
If none of these conditions is true, an error will occur.
Upvotes: 1