已弃用的“GoogleCredential”的替代方法是什么?

Ros*_*eti 14 java google-cloud-storage google-cloud-platform

我一直在使用以下 Java 方法在 GCS 中设置存储桶通知。

private void setBucketNotification(String bucketName, String topicId) {

List<String> eventType = new ArrayList<>();
eventType.add("OBJECT_FINALIZE");

try {
  Notification notification = new Notification();
  notification.setTopic(topicId);
  notification.setEventTypes(eventType);
  notification.setPayloadFormat("JSON_API_V1");

  final GoogleCredential googleCredential = GoogleCredential
      .fromStream(Objects.requireNonNull(classloader.getResourceAsStream("Key.json")))
      .createScoped(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL));  

  final com.google.api.services.storage.Storage myStorage = new com.google.api.services.storage.Storage.Builder(
      new NetHttpTransport(), new JacksonFactory(), googleCredential).build();

  Notification v = myStorage.notifications().insert(bucketName, notification).execute();

} catch (IOException e) {
  log.error("Caught an IOException {}",e);
  }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,它一直运行良好,但最近,我收到了关于GoogleCredential课程弃用的投诉,并尝试进行一些研究,希望找到可能的替代品,但找不到任何东西。谁能帮我指出正确的方向?

Ros*_*eti 30

环顾四周后,我设法修复它,使用GoogleCredentialsHttpRequestInitializer。代码变化如下。

final GoogleCredential googleCredential = GoogleCredential
  .fromStream(Objects.requireNonNull(classloader.getResourceAsStream("Key.json")))
  .createScoped(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL));

final com.google.api.services.storage.Storage myStorage = new com.google.api.services.storage.Storage.Builder(
      new NetHttpTransport(), new JacksonFactory(), googleCredential).build();
Run Code Online (Sandbox Code Playgroud)

变成

final GoogleCredentials googleCredentials = serviceAccountCredentials
                    .createScoped(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL));
            HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(googleCredentials);        

final com.google.api.services.storage.Storage myStorage = new com.google.api.services.storage.Storage.Builder(
                new NetHttpTransport(), new JacksonFactory(), requestInitializer).build();
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,@Roshan!对我来说,棘手的部分是如何从“GoogleCredentials”获取“HttpRequestInitializer”;谷歌搜索把我带到了这里:) (3认同)
  • 如何使用 p12 私钥文件获取服务帐户凭据? (2认同)
  • @insa_c 为什么不使用带有 `ServiceAccountCredentials` 的 `json` 文件,如下所示: `ServiceAccountCredentials.fromStream(Objects.requireNonNull(classloader.getResourceAsStream("Key.json"))) .createScoped(Collections.singletonList(StorageScopes) .DEVSTORAGE_FULL_CONTROL));` (2认同)

sll*_*pis 10

您可以找到发布在Google APIs Github 存储库 commits上的替代解决方案。

请使用Google Auth Library for Java来处理应用程序默认凭据和其他基于非 OAuth2 的身份验证。

显式凭据加载示例代码:

要从服务帐户 JSON 密钥获取凭据,请使用 GoogleCredentials.fromStream(InputStream) 或 GoogleCredentials.fromStream(InputStream, HttpTransportFactory)。请注意,必须在访问令牌可用之前刷新凭据。

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("/path/to/credentials.json"));
credentials.refreshIfExpired();
AccessToken token = credentials.getAccessToken();
// OR
AccessToken token = credentials.refreshAccessToken();
Run Code Online (Sandbox Code Playgroud)

  • 这是一个仅链接的答案 - 尽管它提供了正确的信息,但我强烈希望您可以添加一个有关如何使用 api 的示例(-1) (2认同)