为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

30秒到达战场 2023-08-04 16:29:24
我一直在使用以下 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,并尝试做一些研究,希望找到可能的替代品,但找不到任何东西。谁能帮我指出正确的方向?
查看完整描述

4 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

经过一段时间的环顾后,我设法使用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();


查看完整回答
反对 回复 2023-08-04
?
慕运维8079593

TA贡献1876条经验 获得超5个赞

您可以找到发布在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();



查看完整回答
反对 回复 2023-08-04
?
繁华开满天机

TA贡献1816条经验 获得超4个赞

当使用 Google Admin SDK API 并需要 com.google.api.services.admin.directory.Directory时,我必须解决类似的任务。


    final ServiceAccountCredentials serviceAccountCredentials = ServiceAccountCredentials.fromPkcs8(

            clientId,

            clientEmail,

            serviceAccountPkcs8Key,

            serviceAccountPkcs8Id,

            Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY));

    final GoogleCredentials delegatedCredentials = serviceAccountCredentials.createDelegated(delegatedUserEmail);

    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(delegatedCredentials);


    Directory directory = new Directory.Builder(httpTransport, JSON_FACTORY, requestInitializer)

            .setApplicationName(applicationName)

            .build();


查看完整回答
反对 回复 2023-08-04
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

使用google-auth-library-oauth2-http库


代码:


ServiceAccountCredentials getServiceAccountCredentials(String privateKeyJson) 

throws IOException {

   try (InputStream stream = new ByteArrayInputStream(privateKeyJson.getBytes())) {

      return ServiceAccountCredentials.fromStream(stream);

   }

}

ServiceAccountCredentials serviceAccountCredentials = getServiceAccountCredentials(privateKeyJson);

String privateKeyId = serviceAccountCredentials.getPrivateKeyId();

RSAPrivateKey key = (RSAPrivateKey)  serviceAccountCredentials.getPrivateKey();


查看完整回答
反对 回复 2023-08-04
  • 4 回答
  • 0 关注
  • 184 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信