Google ML Engine Predict => gcloud命令有效,但是Java API范围问题

时间:2018-06-27 07:43:15

标签: java tensorflow google-cloud-platform google-api-java-client

我使用Google ML Engine,并且已经使用此教程部署了模型:https://cloud.google.com/ml-engine/docs/tensorflow/getting-started-training-prediction

使用gcloud CLI进行预测可以正常工作。

下一步,我需要从Java API(本地部署,而不是在GCP中)进行预测。我使用以下示例:https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/mlengine/online-prediction

此外,我发现我需要首先进行授权,因此我尝试了隐式和显式连接,如下所示: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/auth/src/main/java/com/google/cloud/auth/samples/AuthExample.java 两者似乎都起作用。我可以连接,它列出了我的存储桶和已部署的模型。

但是我仍然想念一些设置或配置,这似乎是一个范围问题:

Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "invalid_scope",
  "error_description" : "Empty or missing scope not allowed."
}
    at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
    at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:394)
    at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
    at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
    at com.github.megachucky.kafka.streams.machinelearning.Kafka_Streams_TensorFlow_Serving_Google_ML_Engine_Example.main(Kafka_Streams_TensorFlow_Serving_Google_ML_Engine_Example.java:97)

这是我的代码:

//  static void authExplicit(String jsonPath) throws IOException {
//        // You can specify a credential file by providing a path to GoogleCredentials.
//        // Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
//        credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
//              .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
//        Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
//
//        System.out.println("Buckets:");
//        Page<Bucket> buckets = storage.list();
//        for (Bucket bucket : buckets.iterateAll()) {
//          System.out.println(bucket.toString());
//        }
//      }


    static void authImplicit() {
          // If you don't specify credentials when constructing the client, the client library will
          // look for credentials via the environment variable GOOGLE_APPLICATION_CREDENTIALS.
          Storage storage = StorageOptions.getDefaultInstance().getService();

          System.out.println("Buckets:");
          Page<Bucket> buckets = storage.list();
          for (Bucket bucket : buckets.iterateAll()) {
            System.out.println(bucket.toString());
          }
        }

    public static void main(String[] args) throws Exception {
//      authExplicit("/Users/kai.waehner/Google Drive/Confluent_Kai/kai-waehner-project-8aad9356ffa2.json");
        authImplicit();


        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        Discovery discovery = new Discovery.Builder(httpTransport, jsonFactory, null).build();

        RestDescription api = discovery.apis().getRest("ml", "v1").execute();
        RestMethod method = api.getResources().get("projects").getMethods().get("predict");

        JsonSchema param = new JsonSchema();
        String projectId = "kai-waehner-project-mlengine";
        // You should have already deployed a model and a version.
        // For reference, see https://cloud.google.com/ml-engine/docs/deploying-models.
        String modelId = "census";
        String versionId = "v1";
        param.set(
            "name", String.format("projects/%s/models/%s/versions/%s", projectId, modelId, versionId));

        GenericUrl url =
            new GenericUrl(UriTemplate.expand(api.getBaseUrl() + method.getPath(), param, true));
        System.out.println(url);

        String contentType = "application/json";
        File requestBodyFile = new File("src/main/resources/generatedModels/TensorFlow_Census/test.json");
        HttpContent content = new FileContent(contentType, requestBodyFile);
        System.out.println(content.getLength());

        GoogleCredential credential = GoogleCredential.getApplicationDefault();
        HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
        HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content);

        String response = request.execute().parseAsString();
        System.out.println(response);
      }

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

尝试一下,它应该可以工作:

public String predict() throws Exception {

        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        Discovery discovery = new Discovery.Builder(httpTransport, jsonFactory, null).build();
        RestDescription api = discovery.apis().getRest("ml", "v1").execute();
        RestMethod method = api.getResources().get("projects").getMethods().get("predict");

        JsonSchema param = new JsonSchema();
        String projectId = "projectId";
        // You should have already deployed a model and a version.
        // For reference, see https://cloud.google.com/ml-engine/docs/deploying-models.
        String modelId = "modelId";
        String versionId = "versionId";
        param.set(
                "name", String.format("projects/%s/models/%s/versions/%s", projectId, modelId, versionId));

        GenericUrl url =
                new GenericUrl(UriTemplate.expand(api.getBaseUrl() + method.getPath(), param, true));
        System.out.println(url);
        try {
            String contentType = "application/json";
            File requestBodyFile = new File(OnlinePredictionSample.class.getClassLoader().getResource("input.txt").getFile());
            HttpContent content = new FileContent(contentType, requestBodyFile);
            System.out.println(content.getLength());



            GoogleCredential credential = GoogleCredential.fromStream(OnlinePredictionSample.class.getClassLoader().
                    getResourceAsStream("client_secret.json"))
                    .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));


            HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
            HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content);

            String response = new Gson().toJson(request.execute().parseAsString());
            log.info(response);
            return response;
        } catch (Exception e) {
            log.log(Level.SEVERE, "Cannot Make Rest to ML Google Platform ", e);
        }
        return null;
    }