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();
添加回答
舉報