Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Dialogflow Rest API';未经OAuth'的服务帐户授权;弱点_Java_Google Cloud Platform_Google Oauth_Dialogflow Es - Fatal编程技术网

Java Dialogflow Rest API';未经OAuth'的服务帐户授权;弱点

Java Dialogflow Rest API';未经OAuth'的服务帐户授权;弱点,java,google-cloud-platform,google-oauth,dialogflow-es,Java,Google Cloud Platform,Google Oauth,Dialogflow Es,我正在设置一个远程服务器,希望调用Dialogflow REST API并专门检测意图API 我已经为所有意图和实体设置了一个代理,并且希望远程访问意图。我一直在阅读关于如何与API集成的文章,在本文中我遇到了“附录:无OAuth的服务帐户授权” 我相信我基本上遵守了所有的指示。我可能做错了什么,或者我遗漏了什么 这是我的密码: public class DetectIntentTest { private static final String PROJECTID = "m

我正在设置一个远程服务器,希望调用Dialogflow REST API并专门检测意图API

我已经为所有意图和实体设置了一个代理,并且希望远程访问意图。我一直在阅读关于如何与API集成的文章,在本文中我遇到了“附录:无OAuth的服务帐户授权”

我相信我基本上遵守了所有的指示。我可能做错了什么,或者我遗漏了什么

这是我的密码:

    public class DetectIntentTest {

    private static final String PROJECTID = "myprojectid-****";
    private static final String OAUTH_TOKEN_URI = "https://oauth2.googleapis.com/token";
    private static final String CREDENTIALSFILE = "mycredentialsfile-****-****.json";
    private static final String JWT_BEARER_TOKEN_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer";
    private static final HttpTransport HTTPTRANSPORT = new NetHttpTransport();

public static HttpRequest buildRequest(boolean withoutOauth, String url, String message) throws Exception {
    ServiceAccountCredentials credentials = fetchCredentials();
    String jwt = createJwt(withoutOauth, credentials);
    if (jwt == null) {
        throw new Exception("Could not create a signed jwt token");
    }

    String token = withoutOauth ? jwt : fetchToken(jwt);
    if (token == null) {
        throw new Exception("Could not to retrieve token");
    }

    return HTTPTRANSPORT.createRequestFactory()
            .buildPostRequest(
                    new GenericUrl(url),
                    new JsonHttpContent(JacksonFactory.getDefaultInstance(), message))
            .setHeaders(new HttpHeaders()
                    .setAuthorization("Bearer " + token)
                    .set("Host", host));
}

public static String buildUrl(String uri, String api, String languageCode, String projectId, String sessionId) {
    return "https://dialogflow.googleapis.com/v2/projects/" + projectId + uri + sessionId + ":" + api;
}

 private static String createJwt(boolean withoutOauth, ServiceAccountCredentials credentials) throws Exception {
    long now = System.currentTimeMillis();
    String clientId = credentials.getClientId();
    String privateKeyId = credentials.getPrivateKeyId();
    String serviceAccount = credentials.getClientEmail();
    String oauthTokenURI = credentials.getTokenServerUri().toString();
    RSAPrivateKey privateKey = (RSAPrivateKey) credentials.getPrivateKey();
    Algorithm algorithm = Algorithm.RSA256(null, privateKey);
    return withoutOauth
            ? JWT.create()
                    .withKeyId(privateKeyId)
                    .withIssuer(serviceAccount)
                    .withSubject(serviceAccount)
                    .withIssuedAt(new Date(now))
                    .withExpiresAt(new Date(now + 3600 * 1000L))
                    .withAudience("https://dialogflow.googleapis.com/google.cloud.dialogflow.v2.Agents")
                    .sign(algorithm)
            : JWT.create()
                    .withKeyId(privateKeyId)
                    .withIssuer(serviceAccount)
                    .withSubject(serviceAccount)
                    .withIssuedAt(new Date(now))
                    .withExpiresAt(new Date(now + 3600 * 1000L))
                    .withAudience(oauthTokenURI)
                    .withClaim("target_audience", clientId)
                    .sign(algorithm);
}

public static ServiceAccountCredentials fetchCredentials() throws Exception {
    File credentialFile = new File(ClassLoader.getSystemResource(CREDENTIALSFILE).getFile());
    ServiceAccountCredentials credentials = ServiceAccountCredentials
            .fromStream(new FileInputStream(credentialFile));
    credentials.createScoped(
            "https://www.googleapis.com/auth/dialogflow",
            "https://www.googleapis.com/auth/cloud-platform");
    return credentials;
}

public static String fetchToken(String jwt) throws Exception {
    final GenericData tokenRequest = new GenericData().set("grant_type", JWT_BEARER_TOKEN_GRANT_TYPE).set("assertion", jwt);
    final UrlEncodedContent content = new UrlEncodedContent(tokenRequest);
    final HttpRequestFactory requestFactory = HTTPTRANSPORT.createRequestFactory();
    final HttpRequest request = requestFactory
            .buildPostRequest(new GenericUrl(OAUTH_TOKEN_URI), content)
            .setParser(new JsonObjectParser(JacksonFactory.getDefaultInstance()));
    HttpResponse response = request.execute();
    GenericData responseData = response.parseAs(GenericData.class);
    return (String) responseData.get("id_token");
}

public static void main(String[] args) {

    String api = "detectIntent";
    String uri = "/agent/sessions/";
    String languageCode = "en-US";
    String text = "Hi I would like help";
    String sessionId = UUID.randomUUID().toString();
    String host = "https://dialogflow.googleapis.com";
    String url = DetectIntentTest.buildUrl(uri, api, languageCode, PROJECTID, sessionId);
    JSONObject message = new JSONObject("{"
            + "  \"queryInput\": {"
            + "    \"text\": {"
            + "      \"languageCode\": \"" + languageCode + "\","
            + "      \"text\": \"" + text + "\""
            + "    }"
            + "  }"
            + "}");
    try {
        HttpRequest request = DetectIntentTest.buildRequest(true,host, url, message.toString());
        HttpResponse response = request.execute();
        System.out.println(response.getStatusCode());
        System.out.println(response.getStatusMessage());
        GenericData responseData = response.parseAs(GenericData.class);
        System.out.println(responseData.toString());
    } catch (Exception ex) {
        Logger.getLogger(DetectIntentTest.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}
变量:

    {
"bearerToken": "eyJraWQiOiIxYWEyY2MzYTQwZjUwZT********",

"host": "https://dialogflow.googleapis.com",

"text": "Hi I would like help",

"languageCode": "en-US",

"projectId": "myprojectid-****",

"url": "https://dialogflow.googleapis.com/v2/projects/myprojectid-****/agent/sessions/4a02046c-c1a2-4a35-b680-6e779c6d34b8:detectIntent"
    }
正文:

请求:

    Request{
    method=POST, 
    url=https://dialogflow.googleapis.com/v2/projects/myprojectid-****/agent/sessions/4a02046c-c1a2-4a35-b680-6e779c6d34b8:detectIntent, 
    tags={}
    }
标题:

    Authorization: Bearer eyJraWQiOiIxYWEyY2MzYTQwZjUwZT********
    Host: https://dialogflow.googleapis.com
    Content-Type: application/json; charset=utf-8
输出:

    com.google.api.client.http.HttpResponseException: 401 Unauthorized 

我发现了这个问题,我使用了错误的API名称来检测列表中JWT受众的意图。正确的API名称是“google.cloud.dialogflow.v2.Sessions”

    com.google.api.client.http.HttpResponseException: 401 Unauthorized