5

我有一個Android應用程序,最終會將用戶生成的內容存儲在Google雲端存儲存儲區中。但是我無法從我的應用程序代碼中這樣做。代碼如下所示:從谷歌雲存儲的Android應用程序驗證服務帳戶

JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); 
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 
KeyStore keystore = SecurityUtils.getPkcs12KeyStore(); 
keystore.load(resources.openRawResource(R.raw.secret), "***password***".toCharArray()); 
PrivateKey key = (PrivateKey)keystore.getKey("privatekey", "***password***".toCharArray()); 
credential = new GoogleCredential.Builder() 
.setTransport(httpTransport) 
.setJsonFactory(new JacksonFactory()) 
.setServiceAccountPrivateKey(key) 
.setServiceAccountId("**************@developer.gserviceaccount.com") 
.setServiceAccountScopes(Collections.singleton(StorageScopes.DEVSTORAGE_READ_WRITE)) 
.build(); 
credential.refreshToken(); 
String URI = "https://storage.googleapis.com/"+BUCKET_NAME; 
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); 
GenericUrl url = new GenericUrl(URI); 
HttpRequest request = requestFactory.buildGetRequest(url); 
HttpResponse response = request.execute(); 
String content = response.parseAsString(); 
Log.d("testing", "response content is: " + content); 
new Storage.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("Doubts").build(); 

我收到各種錯誤。其中之一是:

java.security.KeyStoreException: java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found 

官方文檔簡單地忽略了Android應用程序的用例。

+1

binaryking簽署網址>即使這個工作 - 你將你的Android應用內嵌入的私有密鑰/密碼? – Jasper 2015-02-04 08:06:53

回答

1

我建議採取從你的Android客戶端授權請求離開的責任,因爲它不被認爲是「可信」的客戶端。

好的做法遵循將產生一個標識的URL服務器端發送到客戶端,以便後者可以使用它在一個安全的和不透明的方式上傳文件到您的水桶。通過這種方式,您還可以消除來自客戶的自然私人憑據的複雜性和風險。

你可以找到更多有關official docs

相關問題