我正在尝试从我的应用程序获取访问令牌和刷新令牌。我正在使用一键登录,但我似乎无法获得所需的访问和刷新令牌。我尝试使用以下内容: 模块:
@Provides
fun provideOneTapClient(
context: Context
) = Identity.getSignInClient(context)
@Provides
@Named("signInRequest")
fun provideSignInRequest(
app: Application
) = BeginSignInRequest.builder()
.setGoogleIdTokenRequestOptions(
BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
.setSupported(true)
.setServerClientId(app.getString(R.string.web_client_id))
.setFilterByAuthorizedAccounts(true)
.build()
)
.setAutoSelectEnabled(true)
.build()
@Provides
@Named("signUpRequest")
fun provideSignUpRequest(
app: Application
) = BeginSignInRequest.builder()
.setGoogleIdTokenRequestOptions(
BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
.setSupported(true)
.setServerClientId(app.getString(R.string.web_client_id))
.setFilterByAuthorizedAccounts(false)
.build()
)
.build()
@Provides
fun provideGoogleSignInOptions(
app: Application
) = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(app.getString(R.string.web_client_id))
.requestScopes(Scope(CalendarScopes.CALENDAR))
.requestServerAuthCode(app.getString(R.string.web_client_id), true)
.requestEmail()
.build()
@Provides
fun provideGoogleSignInClient(
app: Application,
options: GoogleSignInOptions
) = GoogleSignIn.getClient(app, options)
@Provides
fun provideAuthRepository(
auth: FirebaseAuth,
oneTapClient: SignInClient,
@Named("signInRequest")
signInRequest: BeginSignInRequest,
@Named("signUpRequest")
signUpRequest: BeginSignInRequest,
signInClient: GoogleSignInClient,
usersRef: CollectionReference
): AuthRepository = AuthRepositoryImpl(
auth = auth,
oneTapClient = oneTapClient,
signInRequest = signInRequest,
signUpRequest = signUpRequest,
signInClient = signInClient,
usersRef = usersRef
)
AuthImpl:
override suspend fun oneTapSignInWithGoogle() = flow {
try {
emit(Loading)
val result = oneTapClient.beginSignIn(signInRequest).await()
emit(Success(result))
} catch (e: Exception) {
emit(Failure(e))
}
}
override suspend fun oneTapSignUpWithGoogle() = flow {
try {
emit(Loading)
val result = oneTapClient.beginSignIn(signUpRequest).await()
emit(Success(result))
} catch (e: Exception) {
emit(Failure(e))
}
}
override suspend fun firebaseSignInWithGoogle(googleCredential: AuthCredential) = flow {
try {
emit(Loading)
val authResult = auth.signInWithCredential(googleCredential).await()
val isNewUser = authResult.additionalUserInfo?.isNewUser
emit(Success(isNewUser))
} catch (e: Exception) {
emit(Failure(e))
}
}
视图模型:
override suspend fun oneTapSignInWithGoogle() = flow {
try {
emit(Loading)
val result = oneTapClient.beginSignIn(signInRequest).await()
emit(Success(result))
} catch (e: Exception) {
emit(Failure(e))
}
}
override suspend fun oneTapSignUpWithGoogle() = flow {
try {
emit(Loading)
val result = oneTapClient.beginSignIn(signUpRequest).await()
emit(Success(result))
} catch (e: Exception) {
emit(Failure(e))
}
}
override suspend fun firebaseSignInWithGoogle(googleCredential: AuthCredential) = flow {
try {
emit(Loading)
val authResult = auth.signInWithCredential(googleCredential).await()
val isNewUser = authResult.additionalUserInfo?.isNewUser
emit(Success(isNewUser))
} catch (e: Exception) {
emit(Failure(e))
}
}
登录屏幕:
val launcher = rememberLauncherForActivityResult(StartIntentSenderForResult()) { result ->
if (result.resultCode == RESULT_OK) {
try {
val credentials = viewModel.oneTapClient.getSignInCredentialFromIntent(result.data)
val googleIdToken = credentials.googleIdToken
val googleCredentials = getCredential(googleIdToken, null)
viewModel.signInWithGoogle(googleCredentials)
} catch (it: ApiException) {
print(it)
}
}
}
fun launch(signInResult: BeginSignInResult) {
val intent = IntentSenderRequest.Builder(signInResult.pendingIntent.intentSender).build()
launcher.launch(intent)
}
when (val oneTapSignInResponse = viewModel.oneTapSignInState.value) {
is Loading -> ProgressBar()
is Success -> oneTapSignInResponse.data?.let {
LaunchedEffect(it) {
launch(it)
}
}
is Failure -> oneTapSignInResponse.e?.let {
LaunchedEffect(Unit) {
print(it)
if (it.message == "16: Cannot find a matching credential.") {
viewModel.oneTapSignUp()
}
}
}
}
when (val oneTapSignUpResponse = viewModel.oneTapSignUpState.value) {
is Loading -> ProgressBar()
is Success -> oneTapSignUpResponse.data?.let {
LaunchedEffect(it) {
launch(it)
}
}
is Failure -> oneTapSignUpResponse.e?.let {
LaunchedEffect(Unit) {
print(it)
}
}
}
when (val signInResponse = viewModel.signInState.value) {
is Loading -> ProgressBar()
is Success -> signInResponse.data?.let { isNewUser ->
if (isNewUser) {
LaunchedEffect(isNewUser) {
viewModel.createUser()
}
} else {
LaunchedEffect(Unit) {
navController.navigate(EisenhowerScreens.EisenHomeScreen.name) {
popUpTo(EisenhowerScreens.LoginScreen.name) {
inclusive = true
}
}
}
}
}
is Failure -> signInResponse.e?.let {
LaunchedEffect(Unit) {
print(it)
}
}
}
我正在为该应用程序使用 Jetpack Compose。而且我不确定我需要什么才能获得访问和刷新令牌。