亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

已棄用的“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 關注
  • 389 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號