filmov
tv
How to implement Google SignIn OAuth2 authentication in your App? - Android Studio Java | API 34
Показать описание
It also shows the steps to create a new project in the Google's cloud platform and create OAuth2.0 credentials. Please note that for the implementation to work, one needs to create both web client and Android application type OAuth 2.0 credentials. Though the in the code only web application type client ID would be required for directing the authentication.
Complete source code and other details/ steps of this video are posted in the below link:
However, the main Java code is copied below also for reference:
public class MainActivity extends AppCompatActivity {
private TextView textView;
private SignInClient oneTapClient;
private BeginSignInRequest signInRequest;
private static final int REQ_ONE_TAP = 100;
protected void onCreate(Bundle savedInstanceState) {
.setSupported(true)
.build())
.setSupported(true)
// Your server's client ID, not your Android client ID.
// Only show accounts previously used to sign in.
.setFilterByAuthorizedAccounts(false)
.build())
// Automatically sign in when exactly one credential is retrieved.
.setAutoSelectEnabled(true)
.build();
}
public void buttonGoogleSignIn(View view){
.addOnSuccessListener(this, new OnSuccessListener-BeginSignInResult-() {
@Override
public void onSuccess(BeginSignInResult result) {
try {
startIntentSenderForResult(
null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
}
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
// No saved credentials found. Launch the One Tap sign-up flow, or
// do nothing and continue presenting the signed-out UI.
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case REQ_ONE_TAP:
try {
if (idToken != null) {
// Got an ID token from Google. Use it to authenticate
// with your backend.
Log.d(TAG, "Got ID token.");
} else if (password != null) {
// Got a saved username and password. Use them to authenticate
// with your backend.
Log.d(TAG, "Got password.");
}
} catch (ApiException e) {
}
break;
}
}
}
--
Комментарии