4 回答
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();
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();
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();
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();
添加回答
举报