gpt4 book ai didi

java - 已弃用的 'GoogleCredential' 的替代方案是什么?

转载 作者:行者123 更新时间:2023-11-29 08:21:13 25 4
gpt4 key购买 nike

我一直在使用以下 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);
}
}

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

最佳答案

环顾四周后,我设法使用 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();

成为

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();

关于java - 已弃用的 'GoogleCredential' 的替代方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57972607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com