【问题标题】:Google Drive Android API: cannot find file using queryGoogle Drive Android API:无法使用查询找到文件
【发布时间】:2016-06-27 17:24:04
【问题描述】:

在我的应用中,我连接到 Google Api:

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_FILE)
    .addScope(Drive.SCOPE_APPFOLDER)
    .addConnectionCallbacks(mGDriveCallback)
    .addOnConnectionFailedListener(mGDriveCallback)
    .build();
mGoogleApiClient.connect();

然后在mGDriveCallbackonConnected函数中提出查询请求:

Query query = new Query.Builder()
    .addFilter(Filters.eq(SearchableField.TITLE, "buffer.txt"))
    .build();
Drive.DriveApi.query(mGoogleApiClient, query).setResultCallback(mFindFileCallback);

然后在onResultmFindFileCallback 中检查结果:

if (!result.getStatus().isSuccess()) {
    Log.w(TAG, "Problem while retrieving files");
} else {
    Log.i(TAG, "Results size: " + result.getMetadataBuffer().getCount());
    if (result.getMetadataBuffer().getCount() > 0) {
        Metadata md = result.getMetadataBuffer().get(0);
        Log.i(TAG, "DriveId: " + md.getDriveId());
    } else {
        Log.w(TAG, "File not found");
    }
}

现在,即使我个人在我的驱动器上上传了“buffer.txt”,我仍收到空结果的元缓冲区。因此,代码显示“找不到文件”警告。如果我试图找到一个文件夹,也会发生同样的情况。我尝试用Filters.contains 替换Filters.eq,但没有任何成功。

任何想法我做错了什么?

【问题讨论】:

  • 创建文件后,是否明确将标题设置为“buffer.txt”?
  • 我已经上传了文件,然后我尝试重命名它 - 没有成功。我创建了一个文件夹 - 也无法获取它。
  • 文件是否由您的应用上传到驱动器?或者您是否以其他方式上传了文件(例如 Drive Web 界面或 Drive android 应用)?
  • 我已经从网页界面上传了。
  • 这就解释了。 FILE 范围只会让您访问应用程序创建或访问的文件。见这里:developers.google.com/drive/v2/web/scopes。 DRIVE 范围将允许您访问所有文件,但 Android API 不支持它,因此可能需要使用 REST API。

标签: android google-drive-api google-drive-android-api


【解决方案1】:

基于Official Google Documentation当你遇到File not found错误意味着用户没有文件的读取权限或文件不存在。

{
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "File not found {fileId}"
}
],
"code": 404,
"message": "File not found: {fileId}"
}
}

建议的操作:向用户报告他们没有文件的读取权限或文件不存在。告诉他们应该向所有者请求文件的权限。

注意:确保在发出 GET 文件元数据请求时提供正确的 access_token

这是一个相关的 SO 票:Drive API for Android unable to access the file created by same App

【讨论】:

  • 我对 android 完全陌生,我不确定我是否理解“找不到文件”与我的错误的关系。本质上,我想做的是根据文件名获取文件的 DriveId。该文件是我自己创建的,我使用自己的谷歌帐户从应用程序访问它。然而,查询返回没有找到文件。您是否建议即使身份验证没有返回任何错误,我也无法通过我的应用访问我自己的 Gdrive 文件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 2021-07-15
  • 1970-01-01
  • 2012-06-11
  • 2022-01-03
相关资源
最近更新 更多