3
我正在嘗試編寫一個程序,使用Google Drive API在我的Google雲端硬盤中列出這些文件。谷歌驅動器文件列表API返回一個空的數組項目
我已爲我的帳戶創建了一個服務帳戶。
這裏是我一直在使用這個服務對象我叫的File.List
private static List<File> retrieveAllFiles(Drive service)
throws IOException {
List<File> result = new ArrayList<File>();
Files.List request = service.files().list();
do {
try {
FileList files = request.execute();
System.out.println(files);
result.addAll(files.getItems());
request.setPageToken(files.getNextPageToken());
} catch (IOException e) {
System.out.println("An error occurred: " + e);
request.setPageToken(null);
}
} while (request.getPageToken() != null
&& request.getPageToken().length() > 0);
// System.out.println(result);
return result;
}
創建
public static Drive getDriveService() throws GeneralSecurityException,
IOException, URISyntaxException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(
"https://www.googleapis.com/auth/drive")
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.build();
Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
.setApplicationName("FileListAccessProject")
.setHttpRequestInitializer(credential).build();
return service;
}
服務對象,但是這是返回一個空的Items數組
{"etag":"\"8M2pZwE_fwroB5BIq5aUjc3uhqg/vyGp6PvFo4RvsFtPoIWeCReyIC8\"",
"kind":"drive#fileList","selfLink":"https://www.googleapis.com/drive/v2/files",
"items":[]}
有人可以幫我這個,我錯過了什麼嗎?
謝謝。
由於將嘗試了這一點。 – user1586121 2013-03-08 18:20:18