"Cannot find DriveId. Are you authorized to view this file?"

"Cannot find DriveId. Are you authorized to view this file?"

所以我刚刚下载了 Git 中的 Drive Api 示例,以便轻松了解 Drive,但它不适用于原始和未修改的代码。一直向我发送相同的消息: "Cannot find DriveId. Are you authorized to view this file?"

即使我检索、查询或列出内容,此消息也是一样的。我在一些帖子中寻找过这个问题,但找不到解决这个特定问题的方法。任何帮助将不胜感激!

    public class QueryFilesInFolderActivity extends BaseDemoActivity {

    private ListView mResultsListView;
    private ResultsAdapter mResultsAdapter;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_listfiles);
        mResultsListView = (ListView) findViewById(R.id.listViewResults);
        mResultsAdapter = new ResultsAdapter(this);
        mResultsListView.setAdapter(mResultsAdapter);
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        super.onConnected(connectionHint);
        Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FOLDER_ID)
                .setResultCallback(idCallback);
    }

    final private ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
        @Override
        public void onResult(DriveIdResult result) {
            if (!result.getStatus().isSuccess()) {
                showMessage("Cannot find DriveId. Are you authorized to view this file?");
                return;
            }
            DriveId driveId = result.getDriveId();
            DriveFolder folder = driveId.asDriveFolder();
            Query query = new Query.Builder()
                    .addFilter(Filters.eq(SearchableField.TITLE, "Filmoteca"))
                    .build();
            folder.queryChildren(getGoogleApiClient(), query)
                    .setResultCallback(metadataCallback);
        }
    };

    final private ResultCallback<MetadataBufferResult> metadataCallback = new
            ResultCallback<MetadataBufferResult>() {
        @Override
        public void onResult(MetadataBufferResult result) {
            if (!result.getStatus().isSuccess()) {
                showMessage("Problem while retrieving files");
                return;
            }
            mResultsAdapter.clear();
            mResultsAdapter.append(result.getMetadataBuffer());
            showMessage("Successfully listed files.");
        }
    };
}

我发现了问题,事实上它很明显,但这让我疯狂了一个星期!

要解决此问题,您应该先使用 GoogleApi客户端登录(您可以使用 Developers 主页中的演示或 github),那么您需要为要访问的文件指定一个 ID。 接下来,您将能够使用 Drive Api 查询、检索或执行任何您想做的事情。虽然我认为它 API 开始有点复杂,需要一些耐心。

提示:如果您在您的云端硬盘帐户中手动删除了一个文件并且仍然在您的应用程序中看到它,请不要生气,云端硬盘直到 10 或 15 分钟才会同步,也许有一种方法可以强制同步,但我没有没找到。