在我的 Android 应用程序中,我正在访问 Google 云存储。我已经生成了私钥 xxxxxxxkey.p12。我已将密钥文件放在资产文件夹中。但在运行项目时,它不会打开 key.p12 文件。我已经尝试将其放在资产文件夹之外,仍然没有结果。
httpTransport = AndroidHttp.newCompatibleTransport();
AssetManager am = getAssets();
InputStream inputStream = am.open("xxxxxxxxxxKey.p12");
File file = createFileFromInputStream(inputStream);
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com")
.setServiceAccountScopes(Collections.singleton(STORAGE_SCOPE))
.setServiceAccountPrivateKeyFromP12File(file).build();
Run Code Online (Sandbox Code Playgroud)
从输入流创建文件()
private File createFileFromInputStream(InputStream inputStream) {
try {
File f = new File("download/MyKey.p12");
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
inputStream.close();
return f;
} catch (IOException e) …Run Code Online (Sandbox Code Playgroud) java android assets google-cloud-storage google-cloud-platform