无法下载通过我的谷歌驱动器应用程序创建的文件,但可以得到文件的元数据我的、文件、无法下载、驱动器

2023-09-06 11:38:41 作者:孤赌一注

我跟所有的谷歌驱动器SDK中提到的步骤。我建立了我的设备上的示例应用程序(机器人,运行果冻豆)和我能够上传到驱动文件。当试图下载同一个文件,我能够得到这样FILEID,fileTitle,fileDownloadURL等元数据,但无法下载的内容。我收到401未授权错误。

I followed all the steps mentioned in google drive sdk. I created a sample application on my device(android, running jelly bean) and am able to upload a file on to drive. When trying to download the same file, I am able to get the meta data like fileID, fileTitle, fileDownloadURL etc but not able to download the content. I get 401 Unauthorized error.

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

My app AUTH SCOPE is AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/drive.file";

我做下面让OAuth的令牌:

I am doing the following to get the OAUTH Token:

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

基于令牌这是我建立Drive对象

Based on the token this is how I build the Drive object

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

我可以用下面的code上传的文件:

I am able to upload the file using the following code:

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

    }

在试图下载该应用程序上传同一文件,我得到一个401未经授权错误在这行的Htt presponse RESP = service.getRequestFactory()。buildGetRequest(URL).execute( )从以下code段

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

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

Can anyone help me out and let me know what I am doing wrong???

推荐答案

这是一个已知的问题,将与的谷歌播放服务的API。

This is a known issue that will be resolved with the release of the Google Play Services APIs.

由于您的应用程序被授权为 https://www.googleapis.com/auth/drive.file 的范围,并下载终端不支持?关键= 查询参数,也没有办法为我们的服务器知道哪些项目发出请求(以确保应用程序有权限读取这个文件的内容)。

Since your application is authorized for the https://www.googleapis.com/auth/drive.file scope, and the download endpoint doesn't support the ?key= query parameter, there is no way for our server to know which project is issuing the request (to make sure the app has authorization to read this file's content).

在此期间,唯一的解决方法,我可以推荐使用范围广泛: https://www.googleapis.com/auth/drive 。请仅使用在开发应用程序,并等待谷歌播放服务发布。

In the meantime, the only workaround I can recommend is using the broad scope: https://www.googleapis.com/auth/drive. Please use only that while developing your application and waiting for the Google Play Services to be released.

要了解更多关于如何将能够使用Android的新授权的API,你可能有兴趣在这些2谷歌I / O会谈:的构建Android应用程序的使用的Web API的和 编写高效的驱动器应用程序为Android

To learn more about how you will be able to use the new authorization APIs in Android, you might be interested in those 2 Google I/O talks: Building Android Applications that Use Web APIs and Writing Efficient Drive Apps for Android