Java中的Google API身份验证,无需使用环境变量 [英] Google API authentication in Java without using environment variable
问题描述
我已经设置了一个简单的测试应用,以便与Google的 Natural Language API 进行互动。我创建了一个服务帐户,并下载了JSON凭据。我在本地开发机器上运行,因此我将 GOOGLE_APPLICATION_CREDENTIALS
环境变量设置为指向JSON文件。为了清楚起见,这个工作成功:应用程序成功进行了一些API调用并显示结果。
我想删除对环境变量。如何在应用程序中使用JSON文件的已知位置(或任何其他方法)用这些凭证创建 LanguageServiceClient
?
<您可以注册如下:
pre $
DatastoreOptions选项=> DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setAuthCredentials(AuthCredentials.createForJson(
new FileInputStream(PATH_TO_JSON_KEY)))。build();
这有帮助吗?
I have set up a simple test app to interact with Google's Natural Language API. I created a service account, and downloaded the JSON credentials. I am running on a local development machine, so I set the GOOGLE_APPLICATION_CREDENTIALS
environment variable to point to the JSON file. To be clear, this works: the app successfully makes some API calls and displays the results.
I would like to remove the dependence on the environment variable. How can I use the known location of the JSON file (or any other approach) in the application to create the LanguageServiceClient
with those credentials?
You can register is like this:
DatastoreOptions options = DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setAuthCredentials(AuthCredentials.createForJson(
new FileInputStream(PATH_TO_JSON_KEY))).build();
Does that help?
这篇关于Java中的Google API身份验证,无需使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!