【问题标题】:Google Cloud CLOUD NATURAL LANGUAGE API谷歌云自然语言 API
【发布时间】:2018-03-06 16:33:10
【问题描述】:

我正在尝试使用 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 中在哪里设置环境变量或在哪里???

【问题讨论】:

    标签: google-api wildfly


    【解决方案1】:

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

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

      • 双击您的服务器打开 Wildfly 服务器设置 服务器标签
      • 点击“打开启动配置”
      • 移至“环境”选项卡并添加新变量作为键值

      例如

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

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

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

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

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

    就是这样。你很高兴......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多