java - Dialogflow v2 Java 客户端库在 Eclipse 上使用 Spring Boot 检测 Intent

标签 java eclipse spring-boot google-api dialogflow-es

我在 Eclipse 中使用 Dialogflow v2 Java 客户端库和 Spring-Boot 时遇到了困难。

请注意,我已经在 Stack Overflow 上看到过有关这些方法的帖子 api.ai v1 client libraries和乐于助人的谷歌links关于如何进行这些 api 调用。我很笨,请帮忙。

具体来说,我正在尝试发送用户输入以从我的 Dialogflow 代理调用意图,并​​且我已经创建了一个 RestController 来使用类定义中放置的 @RestController 注释来执行此操作。

出于某种原因,即使我已创建服务帐户并在 GCP 帐户中为其选择了 Dialogflow 角色,我仍然收到 PERMISSION_DENIED 错误。

我将按以下顺序发布我的代码:

  • 我的 Spring Rest Controller 类
  • 我的 Spring 应用类
  • 我的 Spring pom.xml
  • Tomcat 服务器在控制台中引发的异常
  • 当我 ping 映射的网址时,我的 Google Chrome 浏览器上打印出异常

我的网页上的异常建议转到此 link启用权限,但当我访问它时,页面只显示“API“dialogflow.googleapis.com”不存在,或者您无权访问它 追踪号码:3690215267179057879"

我的 Spring Rest Controller 类

 package MY_PACKAGE;

import java.io.IOException;
import java.util.UUID;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.cloud.dialogflow.v2.DetectIntentResponse;
import com.google.cloud.dialogflow.v2.QueryInput;
import com.google.cloud.dialogflow.v2.SessionName;
import com.google.cloud.dialogflow.v2.SessionsClient;
import com.google.cloud.dialogflow.v2.TextInput;
import com.google.cloud.dialogflow.v2.TextInput.Builder;

@RestController
public class DialogflowRestController {

    private String userText = "hello";
    private final String LANG_CODE = "en-US";
    private final String PROJECT_ID = "MY_PROJECT_ID";
    private String sessionId = UUID.randomUUID().toString();
    private final String credential = "MY_PATH_TO_GOOGLE_JSON_CREDENTIAL_FILE";
    private final String URL = "https://dialogflow.googleapis.com/v2/{session=projects/MY_PROJECT_ID/agent/sessions/" +
            sessionId + "}:detectIntent";

    @RequestMapping("/intent")
    public String doThings() throws IOException {

        try (SessionsClient sessionsClient = SessionsClient.create()) {
            SessionName session = SessionName.of(MY_PROJECT_ID, sessionId);

            Builder textInput = TextInput.newBuilder().setText(userText).setLanguageCode(LANG_CODE);

            QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build();

            DetectIntentResponse response = sessionsClient.detectIntent(session, queryInput);
            return response.toString();
        }
    }
}

我的 Spring 应用类

package MY_PACKAGE;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DialogflowService {

    public static void main(String[] args) {
        SpringApplication.run(DialogflowService.class, args);

    }

}

我的 Spring pom.xml

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>MY GROUP ID</groupId>
      <artifactId>MY ARTIFACT ID</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>MY PROJECT NAME</name>

      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
      </parent>

      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

       <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-dialogflow</artifactId>
        <version>0.46.0-alpha</version>
    </dependency>

       <dependency>
         <groupId>com.google.api-client</groupId>
         <artifactId>google-api-client</artifactId>
         <version>1.23.0</version>
       </dependency>
      </dependencies>
      <properties>
        <java.version>1.8</java.version>
      </properties>
    </project>

Tomcat服务器在控制台中引发的异常

io.grpc.StatusRuntimeException: PERMISSION_DENIED: Dialogflow API has not been used in project 764086051850 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/dialogflow.googleapis.com/overview?project=764086051850 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
    at io.grpc.Status.asRuntimeException(Status.java:526) ~[grpc-core-1.10.1.jar:1.10.1]
    at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:467) ~[grpc-stub-1.10.1.jar:1.10.1]
    at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:41) ~[grpc-core-1.10.1.jar:1.10.1]
    at io.grpc.internal.CensusStatsModule$StatsClientInterceptor$1$1.onClose(CensusStatsModule.java:684) ~[grpc-core-1.10.1.jar:1.10.1]
    at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:41) ~[grpc-core-1.10.1.jar:1.10.1]
    at io.grpc.internal.CensusTracingModule$TracingClientInterceptor$1$1.onClose(CensusTracingModule.java:391) ~[grpc-core-1.10.1.jar:1.10.1]
    at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:475) ~[grpc-core-1.10.1.jar:1.10.1]
    at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:63) ~[grpc-core-1.10.1.jar:1.10.1]
    at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:557) ~[grpc-core-1.10.1.jar:1.10.1]
    at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$600(ClientCallImpl.java:478) ~[grpc-core-1.10.1.jar:1.10.1]
    at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:590) ~[grpc-core-1.10.1.jar:1.10.1]
    at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37) ~[grpc-core-1.10.1.jar:1.10.1]
    at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123) ~[grpc-core-1.10.1.jar:1.10.1]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_152]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_152]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) ~[na:1.8.0_152]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) ~[na:1.8.0_152]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_152]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_152]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_152]

当我 ping 映射的网址时,我的 Google Chrome 浏览器上打印出异常

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Jul 15 10:27:43 PDT 2018
There was an unexpected error (type=Internal Server Error, status=500).
io.grpc.StatusRuntimeException: PERMISSION_DENIED: Dialogflow API has not been used in project 764086051850 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/dialogflow.googleapis.com/overview?project=764086051850 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

最佳答案

所以我得到了一些帮助并找出了问题所在。

未在 Eclipse 中为我的项目设置 GOOGLE_APPLICATION_CREDENTIAL 变量。环境变量路径需要指向我在 Google Cloud Platform 上创建 Dialogflow 服务时收到的 JSON 文件。此文件对发送到我的 Dialogflow V2 代理的客户端请求进行身份验证。

特别感谢来自 Google 的 Lukasz Bieniasz-Krzywiec 和此 Stack Overflow Post展示如何使用 Eclipse 项目设置环境变量。

关于java - Dialogflow v2 Java 客户端库在 Eclipse 上使用 Spring Boot 检测 Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51351028/

相关文章:

java - 如何将鱼尾纹添加到 UMLet?

eclipse - 使用 Eclipse 和 tomcat 问题运行 java web 项目

c - eclipse 中 C 的 Debug模式

java - Eclipse Java EE 新 Maven 项目 : Could not resolve archetype

spring-boot - 带有 Spring Boot 的 ObjectDB

java - 为多个 API 调用配置 spring boot 重试

java - Spring Data Jpa获取没有实体表示的数据

java.lang.NoClassDefFoundError : org/springframework/context/ApplicationContext maven

java - 用于输入配对序列的 Swing 组件

java.util.concurrent.DelayQueue 忽略过期元素