【问题标题】:Google Dialogflow V2 - Authentication - access tokenGoogle Dialogflow V2 - 身份验证 - 访问令牌
【发布时间】:2019-04-28 05:02:44
【问题描述】:

我正在使用 Google Dialogflow V2 API 进行聊天机器人设计。 为了在 Detect Intent API 的请求参数中传递 Authorization 标头,我需要动态访问令牌。

根据官方文档,我们必须在我不想要的机器上安装 gcloud。

我想通过 java 来实现,但我遇到了一个错误。

我已点击链接:https://github.com/googleapis/google-auth-library-java 并为GOOGLE_APPLICATION_CREDENTIALS 设置环境变量

我用过下面的代码sn-p:

String GoogleCredentialsEnv = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("/Users/Downloads/testingbot-29671-d9229dd1e3f9.json"));
    credentials.createScoped(Arrays.asList("https://www.googleapis.com/auth/dialogflow"));

credentials.refreshIfExpired();
AccessToken token = credentials.getAccessToken();

//AccessToken token = credentials.refreshAccessToken();
System.out.println("Token is " + token);

但大多数时候我都会遇到错误

Exception in thread "main" java.io.IOException: Scopes not configured for service account. Scoped should be specified by calling createScoped or passing scopes to constructor.
    at com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:363)
    at com.google.auth.oauth2.OAuth2Credentials.refresh(OAuth2Credentials.java:181)
    at com.google.auth.oauth2.OAuth2Credentials.refreshIfExpired(OAuth2Credentials.java:198)
    at com.Model.demo.getBotResponse(demo.java:60)
    at com.Model.demo.main(demo.java:40) 

【问题讨论】:

    标签: dialogflow-es


    【解决方案1】:

    确保您使用的服务帐户已为“Dialogflow 集成”创建。

    【讨论】:

      【解决方案2】:

      试试:

      GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("/Users/Downloads/testingbot-29671-d9229dd1e3f9.json"));
      
      if (credentials.createScopedRequired()) {
          credentials = credentials.createScoped(Collections.singletonList("https://www.googleapis.com/auth/dialogflow"));
      }
      
      credentials.refreshIfExpired();
      AccessToken token = credentials.getAccessToken();
      
      System.out.println(token.getTokenValue());
      

      【讨论】:

        猜你喜欢
        • 2017-04-29
        • 1970-01-01
        • 2020-04-30
        • 1970-01-01
        • 2017-04-11
        • 2017-02-17
        • 2023-03-17
        • 2014-12-17
        • 2014-03-12
        相关资源
        最近更新 更多