Dialogflow v2 Java Client Library detectIntent with Spring Boot on Eclipse

By | July 16, 2018
Questions:

I am having a hard time with the Dialogflow v2 Java Client Library and Spring-Boot in Eclipse.

Please note, I have seen the Stack Overflow posts regarding these methods with api.ai v1 client libraries and the helpful Google links regarding how these api calls are made. I am dumb, please help.

Specifically, I am trying to send user input to invoke an intent from my Dialogflow agent, and I have created a RestController to do so with the @RestController annotation placed by my class definition.

For some reason, even though I have created a service account and I chose the Dialogflow role for it in my GCP account, I am still receiving a PERMISSION_DENIED error.

I will post my code in the following order:

  • My Spring Rest Controller Class
  • My Spring Application Class
  • My Spring pom.xml
  • The exception raised in the console by the Tomcat server
  • The exception printed on my Google Chrome browser when I ping my mapped url

THE EXCEPTION ON MY WEBPAGE suggests going to this link to enable permission, but when I go to it the page just says “The API “dialogflow.googleapis.com” doesn’t exist or you don’t have permission to access it
Tracking Number: 3690215267179057879″

My Spring Rest Controller Class

 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();
        }
    }
}

My Spring Application Class

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);

    }

}

My 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>

The exception raised in the console by the Tomcat server

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]

The exception printed on my Google Chrome browser when I ping my mapped url

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.
Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *