java - 找不到 DriveId。您有权查看此文件吗? Android 谷歌驱动集成

标签 java android google-drive-api google-drive-android-api

我正在使用来自 github 的以下代码, 但它显示以下错误:

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

我不知道这背后的原因是什么。

我已经阅读了很多关于 Google 驱动器集成的文章,我想将 STRING 保存到 Google 驱动器中并进行检索,我已经使用了这个演示,但它给我错误。请任何人为我的要求提供演示

/**
 * Copyright 2013 Google Inc. All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.gms.drive.sample.demo;

import java.io.IOException;
import java.io.OutputStream;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi.ContentsResult;
import com.google.android.gms.drive.DriveApi.DriveIdResult;
import com.google.android.gms.drive.DriveFile;

/**
 * An activity to illustrate how to edit contents of a Drive file.
 */
public class EditContentsActivity extends BaseDemoActivity {

    private static final String TAG = "EditContentsActivity";

    @Override
    public void onConnected(Bundle connectionHint) {
        super.onConnected(connectionHint);

        final 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;
                }
                DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(), result.getDriveId());
                new EditContentsAsyncTask(EditContentsActivity.this).execute(file);
            }
        };
        Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FILE_ID)
              .setResultCallback(idCallback);
    }

    public class EditContentsAsyncTask extends ApiClientAsyncTask<DriveFile, Void, Boolean> {

        public EditContentsAsyncTask(Context context) {
            super(context);
        }

        @Override
        protected Boolean doInBackgroundConnected(DriveFile... args) {
            DriveFile file = args[0];
            try {
                ContentsResult contentsResult = file.openContents(
                        getGoogleApiClient(), DriveFile.MODE_WRITE_ONLY, null).await();
                if (!contentsResult.getStatus().isSuccess()) {
                    return false;
                }
                OutputStream outputStream = contentsResult.getContents().getOutputStream();
                outputStream.write("Hello world".getBytes());
                com.google.android.gms.common.api.Status status = file.commitAndCloseContents(
                        getGoogleApiClient(), contentsResult.getContents()).await();
                return status.getStatus().isSuccess();
            } catch (IOException e) {
                Log.e(TAG, "IOException while appending to the output stream", e);
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (!result) {
                showMessage("Error while editing contents");
                return;
            }
            showMessage("Successfully edited contents");
        }
    }
}

最佳答案

如消息所述,您的应用需要获得授权才能查看特定文件才能对其进行编辑,因为您的应用拥有 Drive.File scope访问权限。

您可以通过让用户在打开的文件对话框中选择或通过创建文件来获得访问特定文件的授权。

关于java - 找不到 DriveId。您有权查看此文件吗? Android 谷歌驱动集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22350825/

相关文章:

java - 修改文件名时文件保存失败

java - Java8是否支持运算符重载

Android 按钮在 ScrollView 中不可见

android - 如何在微调器的 ArrayAdapter 中添加 ArrayList<My class>?

android - RenderThread 与 UI 线程

google-apps-script - 从电子表格数据行生成文档页面

java - RESTEasy - 某些方法 URL 地址不起作用

java - Jython:无法在 Java 中导入 python 函数

ios - 通过 IOS Google Drive SDK 列出 Google Drive 中指定文件夹中的所有文件

ide - 使用 Google Drive(或任何云存储)作为开发工作区?