【问题标题】:Google Drive File Download谷歌云端硬盘文件下载
【发布时间】:2012-08-29 17:32:53
【问题描述】:

我正在尝试使用下面提到的代码从谷歌驱动器下载文件。 我已经成功测试了其他方法,例如插入文件、删除文件上传文件,但是在尝试下载文件时,我在下面的代码中的 HttpResponse response = request.execute() 处收到 401 授权错误。

private static synchronized InputStream downloadFile(Drive service, File file) throws IOException 
    {
        //File file = service.files().get(fileId).execute();
        String fileURL = file.getDownloadUrl();
        GenericUrl genericUrl = new GenericUrl(fileURL);
        if (fileURL != null && fileURL.length() > 0) 
        {
            try {

                    HttpRequest request = service.getRequestFactory().buildGetRequest(genericUrl);
                    HttpResponse response = request.execute();
                    InputStream inStream = response.getContent();

                    //HttpResponse resp = service.getRequestFactory().buildGetRequest(url).execute();
                    System.out.println("content "+ inStream);
                    return inStream;
            } catch (IOException e) {

                // An error occurred.
                e.printStackTrace();
                System.out.println("downloadFile error :"+ e.getMessage());
                return null;
            }
        } else {
                    // The file doesn't have any content stored on Drive.
                    return null;
        }
    }

我也尝试过使用 Async 实现,但仍然出现 401 错误

08-30 19:31:13.917: W/System.err(1585): com.google.api.client.http.HttpResponseException: 401 Unauthorized
08-30 19:31:13.925: W/System.err(1585):     at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:978)
08-30 19:31:13.935: W/System.err(1585):     at com.rise.risedrive.RiseDriveActivity$DownloadFile.doInBackground(RiseDriveActivity.java:112)
08-30 19:31:13.945: W/System.err(1585):     at com.rise.risedrive.RiseDriveActivity$DownloadFile.doInBackground(RiseDriveActivity.java:1)
08-30 19:31:13.955: W/System.err(1585):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-30 19:31:13.955: W/System.err(1585):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-30 19:31:13.965: W/System.err(1585):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-30 19:31:13.965: W/System.err(1585):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-30 19:31:13.975: W/System.err(1585):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-30 19:31:13.975: W/System.err(1585):     at java.lang.Thread.run(Thread.java:1019)
08-30 19:31:32.250: W/jdwp(1585): Debugger is telling the VM to exit with code=1
08-30 19:31:32.250: I/dalvikvm(1585): GC lifetime allocation: 2603 bytes

我尝试调试它,但不能 帮我解决这个问题

提前致谢

【问题讨论】:

  • 你能打印错误信息吗?您可以从这个page 了解更多关于如何捕获错误的信息。凭据可能已过期,或者用户可能已撤销对您应用的访问权限。

标签: android google-drive-api


【解决方案1】:

您的代码中没有任何地方设置授权标头。

你需要类似的东西

setRequestProperty("Authorization", "OAuth " + "***ACCESS_TOKEN***");

对于任何 401/403 错误,非常值得在 Oauth Playground https://developers.google.com/oauthplayground/ 上处理您的请求,这样您就有了构建应用程序的基准。

【讨论】:

  • service.getRequestFactory().buildGetRequest() 应该在内部这样做。
猜你喜欢
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多