我使用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);
      }

任何帮助赞赏!