Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不推荐使用的';谷歌认证';?_Java_Google Cloud Platform_Google Cloud Storage - Fatal编程技术网

Java 不推荐使用的';谷歌认证';?

Java 不推荐使用的';谷歌认证';?,java,google-cloud-platform,google-cloud-storage,Java,Google Cloud Platform,Google Cloud Storage,我一直在使用以下Java方法在GCS中设置bucket通知 private void setBucketNotification(String bucketName, String topicId) { List<String> eventType = new ArrayList<>(); eventType.add("OBJECT_FINALIZE"); try { Notification notification = new Notification();

我一直在使用以下Java方法在GCS中设置bucket通知

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);
  }
}
private void setBucketNotification(字符串bucketName,字符串topicId){
List eventType=new ArrayList();
添加(“OBJECT_FINALIZE”);
试一试{
通知通知=新通知();
通知.setTopic(topicId);
notification.setEventTypes(eventType);
setPayloadFormat(“JSON_API_V1”);
最终GoogleCredential GoogleCredential=GoogleCredential
.fromStream(Objects.requirennull(classloader.getResourceAsStream(“Key.json”))
.createScope(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL));
final com.google.api.services.storage.storage myStorage=new com.google.api.services.storage.storage.Builder(
新建NetHttpTransport()、新建JacksonFactory()、googleCredential).build();
Notification v=myStorage.notifications().insert(bucketName,Notification.execute();
}捕获(IOE异常){
错误(“捕获到IOException{}”,e);
}
}

到目前为止,它一直运行良好,但最近,我收到了一个关于
GoogleCredential
类被弃用的投诉,并尝试进行一些研究,希望找到一个可能的替代品,但什么也找不到。有人能帮我指出正确的方向吗?

您可以在上找到另一种解决方案

请使用处理应用程序默认凭据和其他非基于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-auth-library-oauth2-http
library

代码:


在使用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();


您在哪里发现
GoogleCredential
已被弃用?也许您需要更改正在使用的库?googleauth库java或com.google.api:google-api-services-oauth2:v1-rev155-1.25。0@JohnHanley与初始化的位置相同。请在问题中包含确切的消息。“我的生成”不显示已弃用的消息。您使用的是哪个Java版本和JDK?@JohnHanley它刚刚说“com.google.api.client.googleapis.auth.oauth2.GoogleCredential不推荐使用”。我不知道我和这件事的区别有多准确和清楚。我使用的是java版本“1.8.0_212”。这是一个只提供链接的答案——尽管它提供了正确的信息,但我强烈希望您能添加一个关于如何使用api(-1)Hey@MartinFrank的示例。你完全正确。可以修改或断开超链接。最好的做法是添加一些示例代码或文本来进一步解释。我已经有一段时间没有发布这个答案了。那是在我刚开始的时候,我没有意识到这样的事情。在这件事上请容忍我。谢谢你指出。我现在就提供一个答案-但是如果你改变你的答案(至少稍微改变一下),我肯定会删除我的-1@MartinFrank Perfect!我相信我编辑的答案现在提供了一个凭证加载的基本示例,以及弃用的
GoogleCredential
类如何变成
GoogleCredentials
谢谢。虽然这个答案显示了使用
GoogleCredentials
的一些方法,但它没有显示如何从中获取
HttpRequestInitializer
,这是使用它替代问题代码中不推荐的
GoogleCredential
所必需的(在@Roshan的回答中有解释)。谢谢,@Roshan!对我来说,棘手的部分是如何从
GoogleCredentials
获取
HttpRequestInitializer
;谷歌搜索它让我来到这里:)你如何使用p12私钥文件获取服务帐户凭据?@insa_c为什么不将
json
文件与
servicecomportcredentials
一起使用,如下所示:
servicecomportcredentials.fromStream(Objects.requirennull(classloader.getresourceastream(“key.json”)).createScope(Collections.singletonList(StorageScopes.DEVSTORAGE_FULL_CONTROL))
HttpCredentialsAdapter
是我所需要的全部,谢谢!
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();
    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();