在Google文档中运行示例程序时,Google Drive Android Api引发错误

时间:2018-10-16 08:06:44

标签: java android google-drive-api drive

引发 com.google.android.gms.common.api.ApiException:17:用户取消了错误解决方案,原始错误消息:INTERNAL_ERROR:空

该怎么办?我以https://developers.google.com/drive/android/appfolder为例。

在Drive Api呼叫之前进行预先安排

  1. GoogleSignIn 成功使用 Drive.APP_FOLDER 范围。但无法与云端硬盘一起使用。

  2. 已在Api控制台中注册并使用凭据启用了Drive Api。

这是我的代码

    private DriveResourceClient getDriveResourceClient(){

            GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);

            return Drive.getDriveResourceClient(this,account);


        }

    private void createFileInAppFolder() {

        final Task<DriveFolder> appFolderTask =getDriveResourceClient().getAppFolder();

        final Task<DriveContents> createContentsTask = getDriveResourceClient().createContents();

        Tasks.whenAll(appFolderTask, createContentsTask)
                .continueWithTask(new Continuation<Void, Task<DriveFile>>() {
                    @Override
                    public Task<DriveFile> then(@NonNull Task<Void> task) throws Exception {

                        DriveFolder parent = appFolderTask.getResult();

                        DriveContents contents = createContentsTask.getResult();
                        OutputStream outputStream = contents.getOutputStream();

                        try  {
                            Writer writer = new OutputStreamWriter(outputStream);
                            writer.write("Hello World!");
                        }
                        catch(Exception e){

                        }

                        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                .setTitle("New file")
                                .setMimeType("text/plain")
                                .setStarred(true)
                                .build();

                        return getDriveResourceClient().createFile(parent, changeSet, contents);

                    }
                })
        .addOnSuccessListener(new OnSuccessListener<DriveFile>() {
            @Override
            public void onSuccess(DriveFile driveFile) {
                Log.e("Success","Good");
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e("Error","Bad",e);
            }
        });
    }

0 个答案:

没有答案