Java API 列出数据流作业

Java API to list dataflow job

我正在尝试编写 java 代码来列出数据流作业。 我参考了 https://cloud.google.com/dataflow/docs/reference/rest/v1b3/projects.jobs/list 但是 'GoogleCredential' Class 已被弃用。我尝试将 GoogleCredential 替换为 GoogleCredentials 但在下面这一行

Dataflow dataflowService = new Dataflow.Builder(httpTransport, jsonFactory, credential)
      .setApplicationName("Google Cloud Platform Sample")
      .build();

它需要 HttpRequestInitializer 而不是凭据。

任何人都可以帮助我如何使用凭证本身并解决这个问题?

需要

HttpRequestInitializer才能在失败时自动重试。可以使用 com.google.api.client.auth.oauth2.Credential 构造一个简单的 HttpRequestInitializer,如下所示:

private HttpRequestInitializer buildInitializer(Credential credential) {
    return httpRequest -> {
      // handles abnormal HTTP responses (non 2XX responses)
      final HttpUnsuccessfulResponseHandler unsuccessfulResponseHandler =
          new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff())
              .setSleeper(Sleeper.DEFAULT);
      /*
      this is designed to work with only one HttpRequest at a time.
      Hence, a new instance of HttpBackOffIOExceptionHandler with new instance of
      BackOff is created for each instance of HttpRequest
      */
      HttpBackOffIOExceptionHandler httpBackOffIOExceptionHandler =
          new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(Sleeper.DEFAULT);
      httpRequest.setInterceptor(credential).setUnsuccessfulResponseHandler(unsuccessfulResponseHandler)
          .setIOExceptionHandler(httpBackOffIOExceptionHandler)
          .setConnectTimeout((int) TimeUnit.MINUTES.toMillis(3))
          .setReadTimeout((int) TimeUnit.MINUTES.toMillis(3))
          .setThrowExceptionOnExecuteError(false).setSuppressUserAgentSuffix(true);

    };
  }