Android - Google Drive 授权 API

问题描述 投票:0回答:1

有人可以帮助使用 Java 中未弃用的工作代码来授权并允许用户与 Google Drive 交互(上传/下载文件)吗?

目前我已经整理了这段代码:

 requestSignIn();

    private void requestSignIn() {
        GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestScopes(new Scope(DriveScopes.DRIVE_FILE))
                .build();

        GoogleSignInClient client = GoogleSignIn.getClient(this, signInOptions);

        Intent signInIntent = client.getSignInIntent();
        someActivityResultLauncher.launch(signInIntent);
    }

final ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    handleSignInInent(result.getData());
                } else {
                    showMsgSnack(getString(R.string.nologin));
                }
            });

    private void handleSignInInent(Intent data) {
        GoogleSignIn.getSignedInAccountFromIntent(data)
                .addOnSuccessListener(googleSignInAccount -> {
                    GoogleAccountCredential credential = GoogleAccountCredential.
                            usingOAuth2(BackupActivity.this, Collections.singleton(DriveScopes.DRIVE_FILE));

                    credential.setSelectedAccount(googleSignInAccount.getAccount());

                    Drive googleDriveService = new Drive.Builder(
                            AndroidHttp.newCompatibleTransport(),
                            new GsonFactory(),
                            credential )
                            .setApplicationName("Hrady")
                            .build();

                    driveServiceHelper = new DriveServiceHelper(googleDriveService);

                    RelativeLayout relativeLayout1 = findViewById(R.id.logscreen);
                    RelativeLayout relativeLayout2 = findViewById(R.id.logmenu);
                    relativeLayout1.setVisibility(View.GONE);
                    relativeLayout2.setVisibility(View.VISIBLE);
                })
                .addOnFailureListener(e -> showMsgSnack(getString(R.string.nologin)));
    }

这工作正常,但是

GoogleSignInClient
GoogleSignIn
已被弃用。

我没有找到任何完整的工作解决方案,我尝试谷歌,ChatGPT,谷歌文档,没有任何帮助来替换我现有的代码。

我尝试过的最接近的解决方案是:

AuthorizationRequest authorizationRequest = AuthorizationRequest
        .builder()
        .setRequestedScopes(Collections.singletonList(new Scope(DriveScopes.DRIVE_FILE)))
        .build();

Identity.getAuthorizationClient(this)
        .authorize(authorizationRequest)
        .addOnSuccessListener(
                authorizationResult -> {
                    if (authorizationResult.hasResolution()) {
                        // access needs to be granted by the user
                        PendingIntent pendingIntent = authorizationResult.getPendingIntent();
                        try {
                            startIntentSenderForResult(pendingIntent.getIntentSender(), 999, null, 0, 0, 0, null);
                        } catch (SendIntentException e) {
                            Log.i("TTTT", "Couldn't start Authorization UI: " + e.getLocalizedMessage());
                        }
                    } else {
                        // access already granted, continue with user action
                        Log.i("TTTT", "access already granted");
                         saveToDriveAppFolder(authorizationResult);
                    }
                })
        .addOnFailureListener(e -> Log.i("TTTT", "Failed to authorize", e));

但是这里

startIntentSenderForResult
已被弃用,何时应授予访问权限并应调用
saveToDriveAppFolder(authorizationResult);
,不知道如何在我现有的代码中反映这一点。

因为在我的代码中

handleSignInInent
我正在使用来自此的凭据:

 GoogleAccountCredential credential = GoogleAccountCredential.
                            usingOAuth2(BackupActivity.this, Collections.singleton(DriveScopes.DRIVE_FILE));

                    credential.setSelectedAccount(googleSignInAccount.getAccount());

                    Drive googleDriveService = new Drive.Builder(
                            AndroidHttp.newCompatibleTransport(),
                            new GsonFactory(),
                            credential )
                            .setApplicationName("MYAPP")
                            .build();

我不能在那里使用

authorizationResult

android google-drive-api
1个回答
0
投票

我也有同样的问题。请考虑向 Google 提供反馈:https://developers.google.com/identity/authorization/android

© www.soinside.com 2019 - 2024. All rights reserved.