【问题标题】:Cannot access Google Drive from 2 different device无法从 2 个不同的设备访问 Google Drive
【发布时间】:2015-03-14 14:46:37
【问题描述】:

我有一个应用程序可以在Google Drive 上创建一个带有一些文本的word 文件。如果我尝试从我创建该文件的设备中访问Google Drive 上的word 文件,那么它可以工作,一切顺利,我可以读写。但是,如果我尝试在具有相同应用程序的不同设备上使用相同的DriveId 访问相同的文件,那么我将无法访问它。我在StackOverflow 上看到了 2 个相关问题,但我无法弄清楚问题所在。任何帮助,将不胜感激。

这是我正在使用的 GoogleApiClient 连接:-

    if(mGoogleApiClient == null)
    {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addScope(Drive.SCOPE_FILE)
                .build();
    }
    mGoogleApiClient.connect();

这是程序代码:-

@Override
public void onConnected(Bundle bundle) {
    msg.Log("onConnected");

            msg.Log(inputText.encodeToString());
            DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient, inputText);
            file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null).setResultCallback(contentsOpenedCallBack);
}

final private ResultCallback<DriveApi.DriveContentsResult> contentsOpenedCallBack = new ResultCallback<DriveApi.DriveContentsResult>() {
    @Override
    public void onResult(DriveApi.DriveContentsResult driveContentsResult) {

        msg.Log("onResult");
        msg.Log("toString ");
        if (!driveContentsResult.getStatus().isSuccess()) {
            Toast.makeText(getBaseContext(), "Wrong Success", Toast.LENGTH_SHORT).show();

        }

            final DriveContents contents = driveContentsResult.getDriveContents();


                    BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
                    StringBuilder builder = new StringBuilder();
                    String line;

                    try {
                        while ((line = reader.readLine()) != null) {
                            builder.append(line);
                        }
                    } catch (IOException e) {
                        msg.Log(e.toString());
                    }

                    String contentsAsString = builder.toString();

                    try {
                        JSONObject json = new JSONObject(contentsAsString);

                        String Text = json.getString("text");


                        SharedPreferences sp = getSharedPreferences("Font", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("value", Text);
                        msg.Log(Password);


                    } catch (Exception e) {
                        msg.Log(e.toString());
                    }

        }

};

这是错误信息:-

java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
        at com.android.server.am.ActivityManagerService.handleIncomingUser(ActivityManagerService.java:13140)
        at android.app.ActivityManager.handleIncomingUser(ActivityManager.java:2038)
        at com.android.providers.settings.SettingsProvider.callFromPackage(SettingsProvider.java:607)
        at android.content.ContentProvider$Transport.call(ContentProvider.java:279)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:273)
        at android.os.Binder.execTransact(Binder.java:388)
        at dalvik.system.NativeStart.run(Native Method)
03-14 21:48:48.885    2341-3805/? E/EnterpriseContainerManager﹕ ContainerPolicy Service is not yet ready!!!
03-14 21:48:48.895  25947-25947/com.gajendraprofile.googledrivesample E/ViewRootImpl﹕ sendUserActionEvent() mView == null
03-14 21:48:50.615   9473-26051/? E/DriveAsyncService﹕ Provided DriveId is invalid.
OperationException[Status{statusCode=Provided DriveId is invalid., resolution=null}]
        at com.google.android.gms.drive.api.e.d(SourceFile:331)
        at com.google.android.gms.drive.api.e.a(SourceFile:632)
        at com.google.android.gms.drive.api.a.ab.a(SourceFile:85)
        at com.google.android.gms.drive.api.a.b.a(SourceFile:27)
        at com.google.android.gms.common.service.c.onHandleIntent(SourceFile:60)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.os.HandlerThread.run(HandlerThread.java:61)

【问题讨论】:

  • 使用developers.google.com/drive/v2/reference/files/get#try-it检查您使用的ID是否有效。检查项目 ID 是否相同,因为您使用的是 drive.file 范围。您是否将 GDAA 返回的 ID 与 REST API 使用的 ID 混淆了,因为它们是不同的。
  • @pinoyyid 感谢您的回复。实际上,我的应用程序上有 2 个类,分别称为 ActivateValidateActivate 类创建一个文件并向用户显示文件DriveId。而当用户点击Validate类时。一个对话框将询问文件DriveId。获取文件DriveId 后,应用程序将从文件中读取数据,该文件位于 Google Drive 上。此过程可以在创建文件的设备上完成。但是当我尝试使用文件DriveId 访问另一台设备上的Validate 类时。然后我无法访问。可能是什么问题?
  • @pinoyyid 我检查了驱动器 ID 是否有效,还检查了项目 ID。问题仍然没有解决。完全卡住了。任何建议表示赞赏。谢谢
  • 阅读stackoverflow.com/questions/29030110/…的答案,确保您使用的是正确的ID
  • @pinoyyid 我 100% 确定驱动器 ID 是正确的。因为从创建文件的设备尝试。它完美地工作。但它不适用于替代设备。请看一下上面的堆栈。我已经更新了它们。谢谢

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


【解决方案1】:

DriveId 在不同的设备上没有任何意义。如果您想使用文件/文件夹,则必须在不同的设备上使用 ResourceId。那是来自 RESTful API 的旧“id”。 DriveId 特定于 Google Play 服务* 的一个特定实例。

您还可以使用标题、mime、父级等元数据搜索文件/文件夹……但是!只有 DriveId 和 ResourceId 是唯一 ID,其他所有内容(包括标题)都不是。这意味着您可以在云端硬盘的同一位置拥有多个同名文件/文件夹!

*) 有人试图在 SO 29030110 上解释这一点。

祝你好运

【讨论】:

  • 如果我想使用 resourceId 从Google Drive 获取数据(在不同设备中使用相同的应用程序)。那我需要使用 Drive REST API 吗?类似于这个例子link?
  • 两个设备上的 ResourceId 相同。您可以使用 GDAA。但是,您仍然必须管理延迟/同步。正如我曾经被告知的那样:“它最终会发生”。顺便说一句,如果您右键单击 > 共享 > 获取可共享链接,您将在 Web 界面中看到 ResourceId。
  • 如果您使用 REST,效果将立竿见影(您仍然需要在 UI 线程外运行它并等待 http 'execute' 请求完成)。
  • 好的,谢谢,我会照你说的试试。
  • DriveId 方法也适用于我。我正在尝试从过去 3 天找到解决此问题的方法。但我找不到任何答案。如果你们中的任何人找到任何替代方法,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-15
  • 2018-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多