【问题标题】:is it possible to batch-authorize a Drive folder and subfolders for access by an Android app?是否可以批量授权驱动器文件夹和子文件夹以供 Android 应用程序访问?
【发布时间】:2017-01-31 08:38:06
【问题描述】:

这是用例...我有一个 Android 应用程序。我希望我的用户将他们 Google Drive 上的一些数据文件组织到一个特定的目录结构中

My Drive/
  appName/
    folderA/
      fileA1
      fileA2
      ...
      fileAN
    folderB/
      fileB1
      fileB2
      ...
      fileBN

我还想让我的用户彼此共享他们的配置。

My Drive/      # User1's drive
  appName/
    folderA/
      fileA1
      fileA2
      ...
      fileAN
    folderB/
      fileB1
      fileB2
      ...
      fileBN

    folderC/    # User2's MyDrive/appName/folderC, which they shared with User1
      fileC1
      fileC2
      ...
      fileCN

这些文件夹中可能包含数百个文件。

我的应用永远不需要删除/重命名/创建文件。 只有需要遍历目录结构,读取文件内容。

我希望用户使用他们已经熟悉的语义来管理他们的数据文件 -- 使用通常的 Google Drive Web 应用程序创建/管理/编辑文件内容。

从我开始阅读的内容...看来 drive.file 范围会导致我计划的工作流程出现问题。来自文档“这意味着只有用户使用您的应用程序打开或创建的文件才能被查询匹配。”

我有哪些选项可以让我的用户指出“此 Android 应用对‘我的云端硬盘/appName’下的文件具有只读访问权限”?

Google Drive Android API 不适合我的用例吗?我应该只关注 Google Drive REST API 吗?

【问题讨论】:

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


    【解决方案1】:

    是的,您可以使用 Google Drive JAVA API 获得更好的运气。在Android Drive api页面(https://developers.google.com/drive/android/queries),据说:

    注意:Android Drive API 仅适用于 https://www.googleapis.com/auth/drive.file 范围。这意味着只有用户使用您的应用程序打开或创建的文件才能被查询匹配。

    您可以按照 Google Drive Java API (https://developers.google.com/drive/v3/web/quickstart/java) 中的指南进行操作。需要进行相当多的修改才能使其在 Android 上运行。为了让您抢先一步,这里有一个关于如何请求 Google Drive 服务的工作代码。您可以将范围更改为只读

    // Retrieve drive service
        com.google.api.services.drive.Drive service = null;
        synchronized (signInLock) {
            try {
                if (googleSignInAccount == null) {
                    Log.d(TAG, "Wait for google sign in");
                    signInLock.wait();
                }
                GoogleAccountCredential credential = GoogleAccountCredential
                        .usingOAuth2(this, Arrays.asList(SCOPES))
                        .setBackOff(new ExponentialBackOff())
                        .setSelectedAccountName(googleSignInAccount.getEmail());
                service = new com.google.api.services.drive.Drive.Builder(
                        AndroidHttp.newCompatibleTransport(),
                        JacksonFactory.getDefaultInstance(), credential)
                        .setApplicationName(Version.APP_NAME)
                        .build();
            } catch (InterruptedException e) {
                Log.d(TAG, "InterruptedException " + e.getMessage());
            } catch (Exception e) {
                Log.d(TAG, "Error while getting drive service " + e.getMessage());
                return;
            }
        }
        if (service == null) {
            Log.d(TAG, "Drive service is null");
            return;
        }
    

    这就是您获得 googleSignInAccount 的方式

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions
                .DEFAULT_SIGN_IN)
                .requestEmail()
                .requestScopes(Drive.SCOPE_FILE)
                .build();
    
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)  // Google Drive Android API
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso) // Google Sign-in Api
                .addConnectionCallbacks(this) // ConnectionCallbacks
                .addOnConnectionFailedListener(this) // OnConnectionFailedListener
                .build();
    
    private void googleSilentSignIn() {
        OptionalPendingResult<GoogleSignInResult> pendingResult = Auth.GoogleSignInApi.silentSignIn(
                mGoogleApiClient);
        if (pendingResult.isDone()) {
            // There's immediate result available.
            handleSignInResult(pendingResult.get());
        } else {
            // There's no immediate result ready, waits for the async callback.
            pendingResult.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                @Override
                public void onResult(@NonNull GoogleSignInResult result) {
                    handleSignInResult(result);
                }
            });
        }
    }
    
    private void handleSignInResult(GoogleSignInResult result) {
        Log.d(TAG, "handleSignInResult: " + result.isSuccess());
        if (result.isSuccess()) {
            googleSignInAccount = result.getSignInAccount();
            synchronized (signInLock) {
                signInLock.notify();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-25
      • 1970-01-01
      • 2014-01-12
      • 2023-03-13
      • 2016-10-07
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      相关资源
      最近更新 更多