【发布时间】: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