Google Cloud云团自然语言API

时间:2017-09-25 16:14:26

标签: google-api wildfly

我正在尝试使用 Google Cloud CLOUD NATURAL LANGUAGE API。

我已经有Google云运行帐户。 我启用了CLOUD NATURAL LANGUAGE API服务并生成了服务帐户密钥并在本地下载。

我使用Goggle默认程序

LanguageServiceClient language = LanguageServiceClient.create();

// The text to analyze
String text = "My stay at this hotel was not so good";
Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();

// Detects the sentiment of the text
Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment();

System.out.printf("Text: %s%n", text);
System.out.printf("Sentiment: %s, %s%n", sentiment.getScore(), sentiment.getMagnitude());

我在 Mac

上使用 Eclipse 作为IDE

当我运行应用程序时出现错误

java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute E
ngine. 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 作为导出,并且在使用“ printenv ”时,它显示了这样的路径

GOOGLE_APPLICATION_CREDENTIALS=/Users/temp/Downloads/Sentiment-0e556940c1d8.json

仍然没有使用一些命中和试用方法我发现在eclipse中我们可以配置运行。

我添加了环境变量,之后运行程序时它运行正常。

现在我的问题是我在J2EE项目中实现该代码,并且该ear文件将在Wildfly中部署。 我再次得到同样的错误。现在我不知道在Wildfly或者???

中设置enviromnet变量的位置

1 个答案:

答案 0 :(得分:0)

最后,我找到了在Wildfly中设置GOOGLE_APPLICATION_CREDENTIALS作为环境变量的方法

  1. 如果您通过Eclipse运行服务器

    • 双击内部服务器打开Wildfly Server设置 服务器选项卡
    • 点击“打开启动配置”
    • 移至“环境”标签,将新变量添加为键值

    例如

    GOOGLE_APPLICATION_CREDENTIALS /Users/temp/Downloads/Sentiment-0e556940c1d8.json

  2. 如果您使用终端运行服务器

    默认情况下,Wildfly会在standalone.conf文件中查找其他设置。

    只需打开 wildfly / bin / standalone.conf 文件并添加以下行

    GOOGLE_APPLICATION_CREDENTIALS = /用户/温度/下载/情感0e556940c1d8.json

  3. 多数民众赞成。你很高兴.....