How do I authenticate my Java application with Google Cloud Storage? - java

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!

Related

Issue with Building Java Application in Docker

I am at the Java Directory. And When I execute the build command:
docker build -t karthikjohnbabu/hello-world-java:0.0.2.RELEASE .
I get the below error message. Please help me???
failed to solve with frontend dockerfile.v0: failed to create LLB definition: no match for platform in manifest sha256:195e9c227ad891282e80602cac2372a3085ecf4ceefbb395558ffe0f7bb0b9aa: not found.
Complete details of error below:
Try adding the following arg to the command:
--platform=linux/amd64
or alternatively use the following command to set an env var:
export DOCKER_DEFAULT_PLATFORM=linux/amd64
The information you gave leaves room for guessing.
I believe the important part is no match for platform in manifest.
Thus I believe in your dockerfile you start FROM someimage, and this someimage is not available for the platform you are using (which could mean MacOS).

GOOGLE_APPLICATION_CREDENTIALS doesn`t work in JAVA

I tried to use Google Speach Api
and I have big problem with authentication.
I followed this tutorial, and I export GOOGLE_APPLICATION_CREDENTIALS, and I checked it using echo, and all ok.
But when I start code from first link I get error:
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.
at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:119)
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:127)
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:100)
at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:53)
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:110)
at com.google.cloud.speech.v1.stub.GrpcSpeechStub.create(GrpcSpeechStub.java:93)
at com.google.cloud.speech.v1.SpeechSettings.createStub(SpeechSettings.java:128)
at com.google.cloud.speech.v1.SpeechClient.<init>(SpeechClient.java:143)
at com.google.cloud.speech.v1.SpeechClient.create(SpeechClient.java:125)
at com.google.cloud.speech.v1.SpeechClient.create(SpeechClient.java:117)
at com.example.speech.QuickstartSample.main(QuickstartSample.java:58)
Of course I checked path in environment variable, even I get this file chmod 777, and nothing help.
anyone have similar problem?
My output for stat $GOOGLE_APPLICATION_CREDENTIALS:
File: /home/kruczek/Pulpit/mirthconnection-59cb32bcd531.json
Size: 2329 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 2360629 Links: 1
Access: (0777/-rwxrwxrwx) Uid: ( 1000/ kruczek) Gid: ( 1000/ kruczek)
Access: 2017-12-01 14:20:33.111439885 +0100
Modify: 2017-12-01 14:20:33.111439885 +0100
Change: 2017-12-01 15:24:26.476545027 +0100
Birth: -
but when i restart computer and i call stat, i have stat: missing operand
my export commend export GOOGLE_APPLICATION_CREDENTIALS=/home/kruczek/Pulpit/mirthconnection-59cb32bcd531.json
and I run project by IDE Intellij, next to privat static void main we have green arrow to run method.
In eclipse,
Run -> Run Configurations.. -> Environment tab
Enter GOOGLE_APPLICATION_CREDENTIALS under Name:
Enter json path in Value:
Make sure you have not entered with quotes when enter path in Value.

Java home path differs

Hello I try to execute my project with bootRun on IntelliJ and I get the followign error:
Execution failed for task ':bootRun'.
> Process 'command '/usr/lib/jvm/java-1.8.0-openjdk- 1.8.0.101-1.b14.fc24.x86_64/bin/java'' finished with non-zero exit value 1
I checked the results of my JAVA paths and stuff and here there are
echo $JAVA_HOME
/home/mypc123/Downloads/jdk1.8.0_101/bin/java
$ which java
/usr/bin/java
I have jdk1.8.0 in /usr/bin
I looked more indepth and found this:
ERROR org.apache.tomcat.jdbc.pool.ConnectionPool - Unable to create initial connections of pool.
org.postgresql.util.PSQLException: FATAL: role "syn12" does not exist
However when I connect to postgresql I have syn12 role and all my gradle JVM's are in the form usr/lib/jvm/java.......
Well we got down to this : Can't load library: /opt/symmetry/ste/java/libste-java.so ,how can I install this library?
Seems like you have your project JDK pointed to another installation. The second issue is most likely different, like an incorrect JDBC url that happens to point to another, existing, database schema in which the required role does not exist.
Intellij IDEA doesn't use the $JAVA_HOME from your system but relies on its own JDK definitions.
It looks like your application is not starting because of the SQL error you found in the logs, and Spring Boot returns 1 because it failed to start.

websphere - CWWKE0054E error unable to open file

i am trying run the websphere liberty profile server from the command line. I am following the steps told here : https://developer.ibm.com/wasdev/downloads/liberty-profile-using-non-eclipse-environments/
I have created the server with the name server1.
But when the extraction completes and I try to start the server using the command : server start server1
the server throws an error : CWWKE0054E: Unable to open file C:\wlp\wlp\usr\servers\server1\logs\C:\Users\Furquan\AppData\Local\Temp\\ihp_custom_batches.log.. Now I know this cant be a valid path, but I dont know where and how to change it. Please help !!
This error is related to the LOG_FILE environment variable that you have defined in your environment by some other program. To solve that, you have the following opions:
Remove LOG_FILE env variable, if it is no longer needed by your system
If you cant do that, override it via server.env file, that you can create in the wlp\usr\servers\serverName directory with the following content:
LOG_FILE=console.log
As last resort (this is not recommended, will make your installation NOT SUPPORTED and in certain installations might get overwritten by updates) - modify the server.bat command line script - in the script find the following section:
if not defined LOG_FILE (
set X_LOG_FILE=console.log
) else (
set X_LOG_FILE=!LOG_FILE!
)
And after the line set X_LOG_FILE=!LOG_FILE! just add another line that will override it with the default like this set X_LOG_FILE=console.log
In general, I'd recommend second solution (with the server.env file), as it is the most portable and will work in any environment.
I have the similar problem for IBM Support Assistant V5. After I deleted %LOG_FILE% from Environment Variables, it worked.

CodeCollaborator error:please see the 'svn upgrade' command

I want to install the tool of CodeCollaborator to manager the code in my development,but there has a install error i cannot handle it.
I trusted that the CodeCollaborator client was installed successful.
When i click the button of add in the CodeCollaborate Client,and then Add CSM Configration successful.
When chose the SCM Cofigration to Add Changes,it always show error as below:
Error initializing changed files
Reason:svn:E155036:please see the 'svn upgarade' command
svn:E155036:Working copy 'E:\woekplace\chunya' is too old(formate 10,create by Subversion 1.6)
I think the error is result by the version between Slik-Subversion-1.7.8-x64 and Subersion in my eclipse, but no matter how i to upgrade the version each other, the error has unswerving exist.How to solve this problem?
Typing svn help upgrade at a Windows command prompt (or svn --help upgrade in a Linux terminal) shows this:
upgrade: Upgrade the metadata storage format for a working copy.
usage: upgrade [WCPATH...]
Local modifications are preserved.
Valid options:
-q [--quiet] : print nothing, or only summary information
Global options:
--username ARG : specify a username ARG
--password ARG : specify a password ARG
--no-auth-cache : do not cache authentication tokens
--non-interactive : do no interactive prompting
--trust-server-cert : accept SSL server certificates from unknown
certificate authorities without prompting (but only
with '--non-interactive')
--config-dir ARG : read user configuration files from directory ARG
--config-option ARG : set user configuration option in the format:
FILE:SECTION:OPTION=[VALUE]
For example:
servers:global:http-library=serf
So it's telling you you need to upgrade your local working copy's metadata, not upgrade SVN itself. You do that also from a command prompt or terminal window, using the syntax shown by the usage example above.

Categories

Resources