Created
June 12, 2024 08:01
-
-
Save NLLAPPS/00f2ad273bc6aa92265a8c1bdd6c2ba6 to your computer and use it in GitHub Desktop.
Migrating Android Google Drive authorization from Deprecated GoogleSignin API to authorization
This file contains 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
import android.app.Activity | |
import android.content.Context | |
import android.content.Intent | |
import android.content.IntentSender.SendIntentException | |
import androidx.core.app.ActivityCompat.startIntentSenderForResult | |
import com.google.android.gms.auth.api.identity.AuthorizationRequest | |
import com.google.android.gms.auth.api.identity.AuthorizationResult | |
import com.google.android.gms.auth.api.identity.Identity | |
import com.google.android.gms.common.api.Scope | |
import com.google.android.gms.tasks.Tasks | |
import com.google.api.client.http.javanet.NetHttpTransport | |
import com.google.api.client.json.gson.GsonFactory | |
import com.google.api.services.drive.Drive | |
import com.google.api.services.drive.DriveScopes | |
import com.google.auth.http.HttpCredentialsAdapter | |
import com.google.auth.oauth2.AccessToken | |
import com.google.auth.oauth2.IdTokenCredentials | |
/** | |
* GoogleSignIn (com.google.android.gms.auth.api.signin.GoogleSignIn) is now deprecated. We are advised to use Authorization for Android ( https://developers.google.com/identity/authorization/android ) | |
* Sample provided there is quite minimal and does not go in to detail on how to proceed after getting the "AuthorizationResult" | |
* This sample shows how to create "Drive" object after getting the "AuthorizationResult" | |
* | |
* Dependencies needed are: | |
* com.google.auth:google-auth-library-oauth2-http | |
* com.google.android.gms:play-services-auth | |
* com.google.http-client:google-http-client-gson | |
* com.google.api-client:google-api-client-android | |
* com.google.apis:google-api-services-drive | |
*/ | |
class GoogleSigningHelper { | |
companion object { | |
private const val tag = "GoogleSigningHelper" | |
const val REQUEST_GOOGLE_SIGN_IN_CODE = 0 | |
private fun getAuthorizationRequest() = AuthorizationRequest | |
.builder() | |
.setRequestedScopes(listOf(Scope(DriveScopes.DRIVE_FILE))) | |
.build() | |
private fun getAccessToken(context: Context): String? { | |
val accessToken = try { | |
val authorizationRequest = getAuthorizationRequest() | |
val task = Identity.getAuthorizationClient(context) | |
.authorize(authorizationRequest) | |
Tasks.await(task).accessToken | |
} catch (e: Exception) { | |
CLog.logPrintStackTrace(e) | |
null | |
} | |
return accessToken | |
} | |
fun getDriveService(context: Context): GoogleDriveAuthResult { | |
val accessTokenString = getAccessToken(context) | |
return if (accessTokenString != null) { | |
val accessToken = AccessToken.newBuilder().setTokenValue(accessTokenString).build() | |
val idTokenCredentials = IdTokenCredentials.create(accessToken) | |
val httpCredentialsAdapter = HttpCredentialsAdapter(idTokenCredentials) | |
GoogleDriveAuthResult.Success( | |
Drive.Builder(NetHttpTransport(), GsonFactory(), httpCredentialsAdapter) | |
.setApplicationName("NLL Drive Client") | |
.build() | |
) | |
} else { | |
GoogleDriveAuthResult.RequireLogin | |
} | |
} | |
fun signOut(context: Context) { | |
//Sign out out is not supported by Identity.getAuthorizationClient | |
} | |
} | |
fun requestSignIn(activity: Activity, onAlreadyConnected: (AuthorizationResult?) -> Unit) { | |
val authorizationRequest = getAuthorizationRequest() | |
Identity.getAuthorizationClient(activity) | |
.authorize(authorizationRequest) | |
.addOnSuccessListener { authorizationResult -> | |
if (authorizationResult.hasResolution()) { | |
// Access needs to be granted by the user | |
try { | |
authorizationResult.pendingIntent?.let { pendingIntent -> | |
startIntentSenderForResult(activity, pendingIntent.intentSender, REQUEST_GOOGLE_SIGN_IN_CODE, null, 0, 0, 0, null) | |
} | |
} catch (e: SendIntentException) { | |
CLog.logPrintStackTrace(e) | |
} | |
} else { | |
onAlreadyConnected(authorizationResult) | |
} | |
} | |
.addOnFailureListener { e -> | |
CLog.logPrintStackTrace(e) | |
} | |
} | |
fun handleSignInResult(activity: Activity, requestCode: Int, resultData: Intent?): AuthorizationResult? { | |
if (requestCode == REQUEST_GOOGLE_SIGN_IN_CODE) { | |
if (resultData != null) { | |
val authorizationResultFromIntent = Identity | |
.getAuthorizationClient(activity) | |
.getAuthorizationResultFromIntent(resultData) | |
return authorizationResultFromIntent | |
} | |
} | |
return null | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment