0

这是这个问题的后续。我已经在 Drive 应用程序文件夹中写入了我的数据文件,现在创建这些文件的同一个应用程序需要检索它们。

这不起作用(我得到文件列表,但文件下载请求返回 401 错误):

private ArrayList<File> listFilesInApplicationDataFolder(Drive service) throws IOException {
  ArrayList<File> result = new ArrayList<File>();
Files.List request = service.files().list();
request.setQ("'appdata' in parents");

do {
  try {
    FileList files = request.execute();

    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);

for (File file:result){
    System.out.println("##### "+file.getOriginalFilename()+ " "+file.toPrettyString());


    if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
          try {
            HttpResponse resp =
                service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()))
                    .execute();
            InputStream in= resp.getContent();
            FileOutputStream fos = openFileOutput("pinmemo",
                    Context.MODE_PRIVATE);
            while(in.available()>0)
                fos.write(in.read());
            fos.close();
          } catch (UserRecoverableAuthIOException e) {
              startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
              return null;
          } catch (IOException e) {
            // An error occurred.
            e.printStackTrace();
            return null;
          }
        } else {
          // The file doesn't have any content stored on Drive.
          return null;
        }


}

return result;

}

我应该怎么办?

编辑:

我收到此错误消息:

04-17 11:19:30.614: W/System.err(2022): com.google.api.client.http.HttpResponseException: 401 Unauthorized
04-17 11:19:30.614: W/System.err(2022): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1095)

编辑:新的谷歌玩游戏服务基本上满足了我的需求(唯一的问题是我的应用不是游戏......),因此这个问题现在已经部分过时了。当然,谷歌玩游戏使用“云保存”而不是“驱动器”,但对于我的需求来说就足够了。但是,我应该将 Google 玩游戏用于非游戏应用吗?

4

1 回答 1

0

确保您的访问令牌具有 appdata 范围的权限:

https://www.googleapis.com/auth/drive.appdata
于 2013-04-17T09:05:48.097 回答