在我的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();
createFileFromInputStream()
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) {
// Logging exception
}
return null;
}
我在Java项目中也做了同样的事情。区别在哪里,是因为Android吗?还是文件位置的路径不正确?
\
替换为Unix路径分隔符/
吗? - bratkartoffel./key.p12
这样简单。双反斜杠只在Java中作为转义字符时才需要使用,因此您需要转义转义字符 ;) - bratkartoffel