无法下载我的应用在Google云端硬盘上创建的文件,但可以获取该文件的元数据

Var*_*.MS 4 android google-drive-api

我按照google drive sdk中提到的所有步骤进行操作.我在我的设备上创建了一个示例应用程序(android,运行果冻豆),并且能够将文件上传到驱动器.当我尝试下载相同的文件时,我能够获取文件ID,fileTitle,fileDownloadURL等元数据,但无法下载内容.我收到401 Unauthorized错误.

我的应用程序AUTH SCOPE是AUTH_TOKEN_TYPE ="oauth2:https://www.googleapis.com/auth/drive.file ";

我正在执行以下操作来获取OAUTH令牌:

AccountManager am = AccountManager.get(this);
        Bundle options = new Bundle();
        am.getAuthToken(
                mAccount,                               
                AUTH_TOKEN_TYPE,                        
                options,                                
                this,                                   
                new OnTokenAcquired(),                  
                new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        invadiateToken();                       
                        super.handleMessage(msg);
                    }
                });  
Run Code Online (Sandbox Code Playgroud)

基于令牌,这是我构建Drive对象的方式

Drive buildService(final String AuthToken) {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
    b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {

        @Override
        public void initialize(JsonHttpRequest request) throws IOException {
            DriveRequest driveRequest = (DriveRequest) request;
            driveRequest.setPrettyPrint(true);
            driveRequest.setKey(API_KEY);
            driveRequest.setOauthToken(AuthToken);
        }
    });

    return b.build();
}
Run Code Online (Sandbox Code Playgroud)

我可以使用以下代码上传文件:

private void uploadLocalFileToDrive(Drive service) throws IOException{

        // File's metadata.
        String mimeType = "text/plain";
        File body = new File();
        body.setTitle("myText.txt");
        body.setDescription("sample app by varun");
        body.setMimeType("text/plain");

        // File's content.
        java.io.File fileContent = new java.io.File(mInternalFilePath);
        FileContent mediaContent = new FileContent(mimeType, fileContent);

        service.files().insert(body, mediaContent).execute();

    }
Run Code Online (Sandbox Code Playgroud)

在尝试下载此应用上传的同一文件时,我HttpResponse resp = service.getRequestFactory().buildGetRequest(url).execute()从以下代码段中获取了此行的401未经授权的错误

private void downloadFileFromDrive(Drive service) throws IOException {
        Files.List request;
            request = service.files().list();
            do {
                FileList files = request.execute(); 
                for(File file:files.getItems()){
                    String fieldId = file.getId();
                    String title = file.getTitle();
                    Log.e("MS", "MSV::  Title-->"+title+"  FieldID-->"+fieldId+" DownloadURL-->"+file.getDownloadUrl());
                    if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0 ) {
                        GenericUrl url = new GenericUrl(file.getDownloadUrl());
                        HttpResponse resp = service.getRequestFactory().buildGetRequest(url).execute();
                        InputStream isd = resp.getContent();
                        Log.e("MS", "MSV:: FileOutPutStream--->"+getFilesDir().getAbsolutePath()+"/downloaded.txt");

                    } else {
                        Log.e("MS", "MSV:: downloadURL for this file is null");
                    }
                }
                request.setPageToken(files.getNextPageToken());
            } while (request.getPageToken()!=null && request.getPageToken().length()>0);
    }
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我,让我知道我做错了什么???

Ala*_*ain 5

这是一个已知问题,将通过发布Google Play服务 API 来解决.

由于您的应用程序已获得https://www.googleapis.com/auth/drive.file范围授权,并且下载端点不支持?key=查询参数,因此我们的服务器无法知道哪个项目正在发出请求(以确保应用程序有权读取此文件的内容) .

与此同时,我可以推荐的唯一解决方法是使用广泛的范围:https://www.googleapis.com/auth/drive.请在开发应用程序时使用该应用程序并等待Google Play服务发布.

要详细了解如何在Android中使用新授权API,您可能会对这两个Google I/O演讲感兴趣:构建使用Web API为Android编写高效驱动应用的Android应用程序