Created
December 7, 2024 02:29
-
-
Save yccheok/640e76f05dc806ba3cb00fdc65933c51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Attempt to perform the silent sign-in: | |
Task<GoogleSignInAccount> task = googleSignInClient.silentSignIn(); | |
2. If step 1 succeeds, we can pass the GoogleSignInAccount to the Google Drive service. | |
3. If step 1 fails, prompt the user to sign in using: | |
startActivityForResult(Utils.buildGoogleSignInClient().getSignInIntent(), ...) | |
By following this approach, the user only needs to sign in once to enable long-term data syncing with Google Drive. | |
To change their signed-in account, the user can simply call: | |
Utils.buildGoogleSignInClient().signOut() | |
and repeat step 1. | |
Implementation of Utils.buildGoogleSignInClient() | |
public static GoogleSignInClient buildGoogleSignInClient() { | |
GoogleSignInOptions signInOptions = | |
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | |
.requestIdToken(GOOGLE_DRIVE_CLIENT_ID) | |
.requestEmail() | |
.requestScopes(new Scope(DriveScopes.DRIVE_APPDATA)) | |
.build(); | |
return GoogleSignIn.getClient(MyApplication.instance(), signInOptions); | |
} | |
Passing GoogleSignInAccount to Google Drive Service (Step 2) | |
private static Drive getDriveService(GoogleSignInAccount googleSignInAccount) { | |
GoogleAccountCredential credential = | |
GoogleAccountCredential.usingOAuth2( | |
MyApplication.instance(), Collections.singleton(DriveScopes.DRIVE_APPDATA) | |
); | |
credential.setSelectedAccount(googleSignInAccount.getAccount()); | |
Drive googleDriveService = | |
new Drive.Builder( | |
AndroidHttp.newCompatibleTransport(), | |
new GsonFactory(), | |
credential) | |
.setApplicationName(APPLICATION_NAME) | |
.build(); | |
return googleDriveService; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment