В настоящее время я собрал этот код:
Код: Выделить всё
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 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)));
}
Я не нашел ни одной полностью работающей решение, я пробовал использовать Google, ChatGPT, документацию Google, ничто не помогло заменить мой существующий код.
Самое близкое решение, которое я пробовал, это следующее:
Код: Выделить всё
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));
Потому что в моем коде в 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();
Подробнее здесь: https://stackoverflow.com/questions/784 ... zation-api
Мобильная версия