Thursday, December 5, 2019

Google Login in Android

Initialization of GoogleSignInOptions, GoogleSignInClient And Button.
    findViewById(R.id.gmail).setOnClickListener(this);
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(activity, gso);
use this on Button Click
     Intent signInIntent = mGoogleSignInClient.getSignInIntent();
     startActivityForResult(signInIntent, 101);
You will get your result onActivityResult method
@Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    if (requestCode == 101) {
        // The Task returned from this call is always completed, no need to attach
        // a listener.
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    } 
    super.onActivityResult(requestCode, resultCode, data);
  }
All process of getting user Data.
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);
        Log.d("sam",account.getDisplayName()+" "+account.getEmail()+" "+account.getId());
        // Signed in successfully, show authenticated UI.
        // ..updateUI(account);
    } catch (ApiException e) {
        // The ApiException status code indicates the detailed failure reason.
        // Please refer to the GoogleSignInStatusCodes class reference for more information.
        Log.d("sam", "signInResult:failed code=" + e.getStatusCode());
    }
   }
Sign Out from App call these method
private void signOut() {
mGoogleSignInClient.signOut()
        .addOnCompleteListener(this, new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                // ...
            }
        });
 }
Gradle config
Project.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

repositories {
    google()//google login
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.2.1'
    classpath 'com.google.gms:google-services:4.0.1'//google login
    

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    google()//google login
    jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
Module gradle
implementation 'com.google.android.gms:play-services-auth:16.0.1'
menifest.xml
//Permisiion
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
Google Developer Link
That is all. If any help related to this post please comment.
Thank you, guys. 
Enjoy coding.

No comments:

Post a Comment