I’m trying to find a current method to authenticate and interact with Google Drive in my Android app. My old code uses GoogleSignInClient and GoogleSignIn, but these are now outdated. I’ve looked everywhere for a new solution without luck.
Here’s a snippet of what I’ve tried:
AuthClient authClient = Identity.getAuthClient(context);
AuthRequest request = AuthRequest.builder()
.setScopes(DriveScopes.DRIVE_FILE)
.build();
authClient.authorize(request)
.addOnSuccessListener(result -> {
if (result.needsUserAction()) {
// Ask user for permission
startActivityForResult(result.getPendingIntent(), AUTH_CODE);
} else {
// Already authorized, proceed
handleDriveAccess(result);
}
})
.addOnFailureListener(e -> Log.e(TAG, "Auth failed", e));
But I’m stuck on how to use this new auth result with my existing Drive service setup. Any ideas on how to connect these parts or a completely new approach?
I’ve recently worked on a similar project and found that the Google Identity Services library is indeed the way to go now. Your approach is on the right track, but you’ll need to make a few adjustments to get it working with Drive.
After obtaining the auth result, you can use it to create a GoogleAccountCredential, which you then use to initialize the Drive service. Here’s a rough outline:
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
context, Collections.singleton(DriveScopes.DRIVE_FILE));
credential.setSelectedAccount(result.getAccount());
Drive driveService = new Drive.Builder(
AndroidHttp.newCompatibleTransport(),
new GsonFactory(),
credential)
.setApplicationName("Your App Name")
.build();
From there, you can use the driveService object to interact with Google Drive. Remember to handle token refreshing and error cases appropriately. Hope this helps point you in the right direction!
I’ve been down this road recently, and I can tell you it’s a bit of a headache. The Google Identity Services library is the new standard, but it’s not exactly user-friendly. Here’s what worked for me:
After getting the auth result, you need to create a GoogleCredentials object. It’s a bit different from the old way:
GoogleCredentials credentials = GoogleCredentials.create(result.getAccessToken());
Then, you can use this to set up your Drive service:
Drive driveService = new Drive.Builder(
new NetHttpTransport(),
GsonFactory.getDefaultInstance(),
new HttpCredentialsAdapter(credentials))
.setApplicationName(“Your App Name”)
.build();
This approach has been more reliable for me than the older methods. Just remember to handle token expiration and refresh properly. It’s a pain, but once you get it working, it’s pretty smooth sailing from there.
hey alice45, i had the same issue. try using the google sign-in api for android. it’s easier to implement. heres a quick example:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(DriveScopes.DRIVE_FILE))
.build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
then use startActivityForResult to launch the sign-in intent. hope this helps!