DriveResource.listParents不起作用不起作用、DriveResource、listParents

2023-09-03 22:49:16 作者:直到舒服为止

我开发的机器人,在那里我需要从谷歌驱动器采取了文件的父母列表中的应用程序。

I am developing an application for Android, where I need to get the list of parents of a file taken from google Drive.

我得到正确使用Drive.DriveApi.newOpenFileActivityBuilder()的DriveId,但是当我使用DriveResource.listParents我得到一个空列表,即使资源有一个父。

I correctly obtain the DriveId using Drive.DriveApi.newOpenFileActivityBuilder(), but when I use DriveResource.listParents I obtain an empty list even if the resource have a parent.

我用的是相同的GoogleApiClient的Drive.DriveApi.newOpenFileActivityBuilder()和DriveResource.listParents,所以我不认为这是一个范围的问题。

I use the same GoogleApiClient for Drive.DriveApi.newOpenFileActivityBuilder() and DriveResource.listParents, so i do not think that is a scope problem.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == Constants.REQUEST_CODE_GOOGLE_DRIVE_FILE_PICKER) {
      if (resultCode == RESULT_OK) {
         mSelectedFileDriveId = (DriveId)    data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
      } else {
         setResult(Constants.RESULT_CODE_KO);
         finish();
      }
   }
}

...

private ResultCallback<MetadataBufferResult> metadataBufferCallback = new ResultCallback<MetadataBufferResult>() {
   @Override
   public void onResult(MetadataBufferResult result) {
      if (!result.getStatus().isSuccess()) {
         SDKUtils.showMessage(thisActivity, "Impossible salvare l'ultima posizione aperta in Google Drive");
          return;
      }
      MetadataBuffer metadataBuffer = result.getMetadataBuffer();
      // HERE I OBTAIN AN EMPTY OBJECT
   }
};

...

mGoogleApiClient = new GoogleApiClient.Builder(this)
              .setAccountName(driveAccontName)
              .addApi(Drive.API)
              .addScope(Drive.SCOPE_FILE)
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this).build();

...

DriveFile driveFile = Drive.DriveApi.getFile(getGoogleApiClient(), mSelectedFileDriveId);
driveFile.listParents(GoogleApiClient apiClient).setResultCallback(metadataBufferCallback);

有什么建议? 谢谢!

Have any suggestions? Thanks!

推荐答案

,所以我不认为这是一个范围的问题。

这正是 - 适用范围的问题!

It's exactly that - Scope problem !!!

谷歌推动Android API只提供drive.file和drive.appfolder范围。

Google Drive Android API provides only drive.file and drive.appfolder scope.

文件范围是指只能访问该用户从应用程序中选择这些文件。该 Drive.DriveApi.newOpenFileActivityBuilder()对话框将让在整个驱动器用户浏览,但您的应用程序将被允许访问只有那些文件或文件夹,用户专门选择的。

File scope means access only to those files which the user chooses from your application. The Drive.DriveApi.newOpenFileActivityBuilder() dialog will let the user browse through the whole drive but your application will be granted access to only those file or folder which the user specifically Select's.

假设你有你的驱动器上的以下文件夹结构 -

Suppose you have following folder structure on your drive -

    /root
      |
      +---books
          +---c++
              +---programming_gems.pdf
              +---c++_notes.docs

现在使用Drive.DriveApi.newOpenFileActivityBuilder()你拿起programming_gems.pdf。 您的应用程序现在已经获得访问该PDF。你可以检索文件及其元数据。 但是,你的应用程序将无法获得在这种情况下获得其父文件夹,C ++。 为什么?因为用户还未获准进入C ++文件夹;内只有一个特定的文件 该文件夹。这是有道理的也;否则,你可以从该文件夹中的所有文件 让你的用户担心。

Now using Drive.DriveApi.newOpenFileActivityBuilder() you pick up programming_gems.pdf. Your app has now obtained access to that pdf. You can retrieve the file and its metadata. But your app won't be able to obtain access to its parent folder, c++ in this case. Why? because the user has not yet given access to c++ folder; only a specific file within that folder. This makes sense also; otherwise you can retrieve any file from that folder leaving your user worried.

使用相同的Drive.DriveApi.newOpenFileActivityBuilder拿起C ++文件夹()。现在,如果你 应用程序尝试检索programming_gems.pdf的父文件夹,它被授予访问权限。

Pick up c++ folder using the same Drive.DriveApi.newOpenFileActivityBuilder(). Now if your app tries to retrieve the parent folder of programming_gems.pdf, it is granted access.

尝试一件事了;尝试列出C的含量++文件夹本身。什么西港岛线返回到 你的应用程序? - 只有programming_gems.pdf。为什么呢,因为用户没有授权访问其他 文件中,C ++ _ notes.docs。

Try one more thing now; try to list the contents of c++ folder itself. What wil be returned to your app? - only programming_gems.pdf. Why, because the user has not granted access to the other file, c++_notes.docs.

我希望很清楚。

P.S。

顺便说一句,要拿起文件或文件夹之间进行选择,你必须设置MIME类型。 请参见下面的code代表如,

By the way, to select between picking up files or folder, you have to set the mime type. See the following code for e.g.,

if (openFolder) {
    mime_type = new String[]{DriveFolder.MIME_TYPE};
} else {
    mime_type = new String[]{};
}

IntentSender intentSender = Drive.DriveApi
        .newOpenFileActivityBuilder()
        .setMimeType(mime_type)
        .build(getGoogleApiClient());

如果作用域这个方案不符合您的要求,您也可以使用谷歌驱动器的REST API - 阅读 - http://stackoverflow.com/a/25871516/1363471