Dialogflow身份验证以编程方式Java

时间:2019-03-26 11:54:40

标签: java sdk dialogflow

嗨,我对dialogflow的身份验证有问题。我知道我必须设置GOOGLE_APPLICATION_CREDENTIALS或下载Gcloud CLI才能访问我的代理。但是由于我要使用多个代理,因此需要使用凭据登录我的API。所以我不想使用这些方法。

我在另一个线程中看到,有一个可用于node.js的代码,该代码正是我想要的。

Dialogflow easy way for authorization

我希望能够处理我下载的json文件,以便获得dialogflow代理的访问权限。

这是我尝试过的:

//Load the json file 
 String credential = "JSON{}"
 //Read the json file
 GoogleCredentials credentials = GoogleCredentials.fromStream(new 
 ByteArrayInputStream(credential.getBytes())); 
 //Read the project ID           
 String projectId = ((ServiceAccountCredentials)credentials).getProjectId();
 System.out.println("the ID"+ projectId);
 //Read the token
 AccessToken token = ((ServiceAccountCredentials)credentials).getAccessToken();
 System.out.println("the token "+ token);

它显示projectID,但令牌为null。我收到的错误是

"message": "The Application Default Credentials are not available. They 
are available if running in Google Compute Engine. 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.",

因此,现在我陷入困境,如何以编程方式连接到其他代理?而不使用Java中的GOOGLE_APPLICATION_CREDENTIALS?

请帮帮我

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我认为这就是您想要的。我已将每个代理配置凭据json的值复制到我自己的配置文件中,并由每个projectId索引,然后从该配置文件读取privatekey,privateKeyId,clientEmail,clientId,TokenServerUri。

然后我用这些值构建一个Credentials对象,然后用凭据构建一个SessionSettings对象

通过这种方式,您可以忘记GOOGLE_APPLICATION_CREDENTIALS环境变量

        PrivateKey privKey = null;

        StringBuilder pkcs8Lines = new StringBuilder();
        BufferedReader rdr = new BufferedReader(new StringReader(privatekey);


        String line;
        while ((line = rdr.readLine()) != null) {
            pkcs8Lines.append(line);
        }

        // Remove the "BEGIN" and "END" lines, as well as any whitespace

        String pkcs8Pem = pkcs8Lines.toString();
        pkcs8Pem = pkcs8Pem.replace("-----BEGIN PRIVATE KEY-----", "");
        pkcs8Pem = pkcs8Pem.replace("-----END PRIVATE KEY-----", "");
        pkcs8Pem = pkcs8Pem.replaceAll("\\s+", "");

        // Base64 decode the result

        byte[] pkcs8EncodedBytes = Base64.getDecoder().decode(pkcs8Pem);

        // extract the private key

        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
        KeyFactory kf;
        try {
            kf = KeyFactory.getInstance("RSA");
            try {
                privKey = kf.generatePrivate(keySpec);
            } catch (InvalidKeySpecException e) {
                throw new GenericException(e);
            }
        } catch (NoSuchAlgorithmException e) {
            throw new GenericException(e);
        }

        Credentials myCredentials = ServiceAccountCredentials.newBuilder().setProjectId(projectId)
                .setPrivateKeyId(privateKeyId).setPrivateKey(privKey)
                .setClientEmail(clientEmail).setClientId(clientId)
                .setTokenServerUri(URI.create(tokenServerUri)).build();

        SessionsSettings sessionsSettings = SessionsSettings.newBuilder()
                .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)).build();

        try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {