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

我一直在使用以下 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,并尝试做一些研究,希望找到可能的替代品,但找不到任何东西。谁能帮我指出正确的方向?


30秒到达战场
浏览 285回答 4
4回答

大话西游666

经过一段时间的环顾后,我设法使用GoogleCredentials和修复了它HttpRequestInitializer。代码改动如下。final GoogleCredential googleCredential = GoogleCredential&nbsp; .fromStream(Objects.requireNonNull(classloader.getResourceAsStream("Key.json")))&nbsp; .createScoped(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL));final com.google.api.services.storage.Storage myStorage = new com.google.api.services.storage.Storage.Builder(&nbsp; &nbsp; &nbsp; new NetHttpTransport(), new JacksonFactory(), googleCredential).build();变成final GoogleCredentials googleCredentials = serviceAccountCredentials&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .createScoped(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(googleCredentials);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;final com.google.api.services.storage.Storage myStorage = new com.google.api.services.storage.Storage.Builder(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new NetHttpTransport(), new JacksonFactory(), requestInitializer).build();

慕运维8079593

您可以找到发布在Google API Github 存储库提交上的替代解决方案。请使用适用于 Java 的 Google Auth 库来处理应用程序默认凭据和其他基于非 OAuth2 的身份验证。显式凭证加载示例代码:要从服务帐户 JSON 密钥获取凭据,请使用 GoogleCredentials.fromStream(InputStream) 或 GoogleCredentials.fromStream(InputStream, HttpTransportFactory)。请注意,在访问令牌可用之前必须刷新凭据。经过一段时间的环顾后,我设法使用GoogleCredentials和修复了它HttpRequestInitializer。代码改动如下。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();

繁华开满天机

当使用 Google Admin SDK API 并需要 com.google.api.services.admin.directory.Directory时,我必须解决类似的任务。&nbsp; &nbsp; final ServiceAccountCredentials serviceAccountCredentials = ServiceAccountCredentials.fromPkcs8(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clientId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clientEmail,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; serviceAccountPkcs8Key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; serviceAccountPkcs8Id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY));&nbsp; &nbsp; final GoogleCredentials delegatedCredentials = serviceAccountCredentials.createDelegated(delegatedUserEmail);&nbsp; &nbsp; HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(delegatedCredentials);&nbsp; &nbsp; Directory directory = new Directory.Builder(httpTransport, JSON_FACTORY, requestInitializer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setApplicationName(applicationName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();

拉莫斯之舞

使用google-auth-library-oauth2-http库代码:ServiceAccountCredentials getServiceAccountCredentials(String privateKeyJson)&nbsp;throws IOException {&nbsp; &nbsp;try (InputStream stream = new ByteArrayInputStream(privateKeyJson.getBytes())) {&nbsp; &nbsp; &nbsp; return ServiceAccountCredentials.fromStream(stream);&nbsp; &nbsp;}}ServiceAccountCredentials serviceAccountCredentials = getServiceAccountCredentials(privateKeyJson);String privateKeyId = serviceAccountCredentials.getPrivateKeyId();RSAPrivateKey key = (RSAPrivateKey)&nbsp; serviceAccountCredentials.getPrivateKey();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java