Reputation: 1445
I am attempting to use the google vision library in java. The steps specify that I need to setup my auth credentials in order to start using the this library . I was able to generate my json property file from API Console Credentials page and I placed it in my spring boot app in the resources folder.
I think updated my application.properties file to include the value like so:
GOOGLE_APPLICATION_CREDENTIALS=datg-avatar-generator-9dc9155cd5bd.json
I'm also setting my property source in my controller like so:
@PropertySource("${GOOGLE_APPLICATION_CREDENTIALS}")
However, after doing that I'm still getting an error saying:
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.
Upvotes: 12
Views: 25083
Reputation: 1
If you are using windows then use
set GOOGLE_APPLICATION_CREDENTIALS="D:\Cloud\service\googleCloudKey.json"
Upvotes: 0
Reputation: 5394
You can load your JSON key file like this:
// Load the JSON key and create a TranslationServiceClient:
public TranslationServiceClient getTranslationClient() throws IOException {
InputStream credentialsStream = getClass().getResourceAsStream("/key.json");
Credentials credentials = GoogleCredentials.fromStream(credentialsStream);
TranslationServiceSettings settings = TranslationServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();
return TranslationServiceClient.create(settings);
}
And this is the usage:
// Replace [YOUR_PROJECT_ID] with your Google Cloud Project ID
public void translateText() {
try (TranslationServiceClient client = getTranslationClient()) {
LocationName parent = LocationName.of("[YOUR_PROJECT_ID]", "global");
TranslateTextRequest request = TranslateTextRequest.newBuilder()
.setParent(parent.toString())
.setMimeType("text/plain")
.setSourceLanguageCode("en-US")
.setTargetLanguageCode("es")
.addContents("Hello, World!")
.build();
TranslateTextResponse response = client.translateText(request);
String translatedText = response.getTranslationsList().get(0).getTranslatedText();
System.out.println(translatedText);
} catch (Exception e) {
e.printStackTrace();
}
}
Remember to be cautious with the key.json file. Ensure it isn't publicly accessible, as misuse can result in unexpected charges on your Google Cloud account.
Upvotes: 0
Reputation: 2390
You need to add env variable
GOOGLE_APPLICATION_CREDENTIALS=<path to google project json file >
If you are using IntelliJ idea, Edit the Project configuration and add the Environment variable
For images check this
https://www.twilio.com/blog/set-up-env-variables-intellij-idea-java
Upvotes: 1
Reputation: 1945
I have tried several ways to do this, and none of them worked. Maven plugin environmentVariables is the last thing that worked without any problem.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<executable>true</executable>
<environmentVariables>
<GOOGLE_APPLICATION_CREDENTIALS>/path/to/the/service-account.json</GOOGLE_APPLICATION_CREDENTIALS>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
Upvotes: 1
Reputation: 603
I was able to configure this property using Spring Cloud GCP spring-cloud-gcp-starter-data-datastore, creating a service account project owner, copy the JSON private key to the main resources directory, and setting the following properties in the application.properties
spring.cloud.gcp.project-id=<project-id>
spring.cloud.gcp.credentials.location=classpath:<credentials-private-key>.json
from the documentation
You can find the project id by visiting this page https://support.google.com/googleapi/answer/7014113?hl=en Go to the API Console.
From the projects list, select Manage all projects. The names and IDs for all the projects you're a member of are displayed. You can also select the project go the settings and see the project ID
Upvotes: 15
Reputation: 5085
For authentication using the service account key, you can set the Environment Variable in your shell.
export GOOGLE_APPLICATION_CREDENTIALS="/Users/username/directory/service-key-file-name.json"
Then you need to start your IDE from the same session. I was stuck after exporting and setting up the environment variable and was still unable to use it.
I tried quitting the current IDE window and restarted the IDE again from the same session. In my case it was Intellij, so in the terminal itself,
cd project directory
idea .
Or you can also add the environment variable in your bash profile and then source it.
Upvotes: 1
Reputation: 570
You can use application properties, but you need to use a different StorageOptions builder.
You are probably using
private static Storage storage = StorageOptions.getDefaultInstance().getService();
But if you want to skip the environment variable you need to use:
Credentials credentials = GoogleCredentials
.fromStream(new FileInputStream("path/to/file"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials)
.setProjectId("my-project-id").build().getService();
Note that the default builder (using environment variables) is better if you are going to deploy your applications to cloud, because then this is automatically filled for you.
Upvotes: 2
Reputation: 2155
You need to set the shell variable. Run this command before mvn run.
export GOOGLE_APPLICATION_CREDENTIALS="/Users/ronnyshibley/Dev/eddress-service-key.json"
Upvotes: 3