How to implement Google SignIn OAuth2 authentication in your App? - Android Studio Java | API 34

Programmer World
Programmer World
9.6 هزار بار بازدید - 7 ماه پیش - In this video it shows
In this video it shows the steps to implement the OAuth2 authentication in your Android App. It refers the code from the Google developer documentation at - https://developers.google.com/identit....

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.

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]

Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/ho...

However, the main Java code is copied below also for reference:

package com.programmerworld.googleoauthauthenticationapp;

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) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       textView = findViewById(R.id.textView);
       oneTapClient = Identity.getSignInClient(this);
       signInRequest = BeginSignInRequest.builder()
               .setPasswordRequestOptions(BeginSignInRequest.PasswordRequestOptions.builder()
                       .setSupported(true)
                       .build())
               .setGoogleIdTokenRequestOptions(BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
                       .setSupported(true)
                       // Your server's client ID, not your Android client ID.
                       .setServerClientId("553874834892-51tqjh0j2m0nren6789bj02h6t2bnquh.apps.googleusercontent.com") // TODO
                       // 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){

       oneTapClient.beginSignIn(signInRequest)
               .addOnSuccessListener(this, new OnSuccessListener-BeginSignInResult-() {
                   @Override
                   public void onSuccess(BeginSignInResult result) {
                       try {
                           startIntentSenderForResult(
                                   result.getPendingIntent().getIntentSender(), REQ_ONE_TAP,
                                   null, 0, 0, 0);
                       } catch (IntentSender.SendIntentException e) {
                           Log.e(TAG, "Couldn't start One Tap UI: " + e.getLocalizedMessage());
                       }
                   }
               })
               .addOnFailureListener(this, new OnFailureListener() {
                   @Override
                   public void onFailure(@NonNull Exception e) {
                       // No saved credentials found. Launch the One Tap sign-up flow, or
                       // do nothing and continue presenting the signed-out UI.
                       Log.d(TAG, e.getLocalizedMessage());
                   }
               });
   }
   @Override
   protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       switch (requestCode) {
           case REQ_ONE_TAP:
               try {
                   SignInCredential credential = oneTapClient.getSignInCredentialFromIntent(data);
                   String idToken = credential.getGoogleIdToken();
                   String username = credential.getId();
                   String password = credential.getPassword();
                   textView.setText("Authentication done.\nUsername is " + username);
                   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) {
                   textView.setText(e.toString());
               }
               break;
       }
   }
}



--
7 ماه پیش در تاریخ 1402/09/16 منتشر شده است.
9,655 بـار بازدید شده
... بیشتر