13

I have integrated sign in with google in my android application, now issue is that in some device its not working,but in my emulator and in some device it is working fine,what is the issue?

https://developers.google.com/identity/sign-in/android/

http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/

this two tutorials to sign in, but when i run my app, and try to login it allows to select account,but when i click on that in my logcat it shows

System.out: Google logout

CODE

private void handleSignInResult(GoogleSignInResult result) {
        Log.d("", "handleSignInResult:" + result.isSuccess());
        if (result.isSuccess()) {
            // Signed in successfully, show authenticated UI.
            GoogleSignInAccount acct = result.getSignInAccount();

            Log.e("", "display name: " + acct.getDisplayName());


            personName = acct.getDisplayName();
            personPhotoUrl = String.valueOf(acct.getPhotoUrl());
            ggoleemail = acct.getEmail();
            googleid = "G" + acct.getId();
          //  String baday = String.valueOf(acct.getAccount());

            Log.e(TAG, "Name: " + personName + googleid  + ", email: " + ggoleemail
                    + ", Image: " + personPhotoUrl);
          /*  txtName.setText(personName);
            txtEmail.setText(email);
            Glide.with(getApplicationContext()).load(personPhotoUrl)
                    .thumbnail(0.5f)
                    .crossFade()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(imgProfilePic);
*/
            googleslogins();
          //  updateUI(true);

        } else {
            // Signed out, show unauthenticated UI.
            //updateUI(false);
            System.out.println("Google logout");
        }
    }
chris
  • 699
  • 4
  • 12
  • 35

6 Answers6

3

Have you added your certificate's hash on Google developer console and turned on the APIs in the Google API manager?

APIs - GooglePlus and People Api

1

First at most, need to check whether google play services version in correctly installed

public static boolean checkPlayServices(Activity context) {
        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = apiAvailability.isGooglePlayServicesAvailable(context);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (apiAvailability.isUserResolvableError(resultCode)) {
                //apiAvailability.getErrorDialog(context, resultCode, 9000).show();
            } else {
                //show error message
            }
            return false;
        }
        return true;
    }

Make sure you have the correct google token. Make sure you enable Google + Sign in in google console

private void setupGoogle() {
        gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.google_token))
                .requestScopes(new Scope(Scopes.PLUS_ME))
                .requestEmail()
                .requestProfile()
                .build();

        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .enableAutoManage(getActivity(), this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .addOnConnectionFailedListener(this)
                .build();
    }

Trigger Sign in

Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, Constant.GG_SIGN_IN);

onActivityResult

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constant.GG_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                firebaseAuthWithGoogle(result.getSignInAccount());
                Logger.debug(new Gson().toJson(result.getSignInAccount()));
                Logger.debug(new Gson().toJson(result.getStatus()));
                //code here
            }
            else{
                Logger.shortToast("google sign-in failed");
            }
        }
    }
ZeroOne
  • 8,996
  • 4
  • 27
  • 45
  • hi,the issue is that in some devices it works and in some devices its not working – chris May 30 '17 at 05:13
  • can you check this one https://stackoverflow.com/questions/48336515/google-analytics-not-refecting-in-fragment – chris Jan 19 '18 at 09:53
1

Follow this steps:

1. Generate SHA1 :

windows:

keytool -list -v -keystore "%USERPROFILE%.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

Mac and Linux:

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

2. Add your project on google console. Add certification by entering SHA1.

3. Enable google sigin from firebase console. Download json file.

4. Add this code to android:

Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
    
    
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                        .requestEmail()
                        .build();
    
       
    
mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
    
    
       
    
     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data)
        {
           super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN)
        {
            GoogleSignInResult result =   Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
       
    }

    private void handleSignInResult(GoogleSignInResult result)
    {
        Log.d("TAG", "handleSignInResult:" + result.isSuccess());
        if (result.isSuccess())
        {
            // Signed in successfully, show authenticated UI.
            GoogleSignInAccount acct = result.getSignInAccount();
            String email = acct.getEmail();
            String name = acct.getDisplayName();
            Uri phone = acct.getPhotoUrl();
            String photo = String.valueOf(phone);

            String[] user_name = name.split("\\s+");

            String fname = user_name[0];

            String lname =user_name[1];

            Log.d("email", email);
            Log.d("photo", photo);
            Log.d("u_name", name);
            Log.d("f_name", fname);
            Log.d("l_name", lname);

        }
    }
Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
1

Based from this thread, you're getting an error maybe because the user's account is attached to both a hosted account and a Google account, and probably it has different passwords for each The authentication servers currently do not handle this well. Follow this Connecting to Google Drive with Google APIs Client Library for Java tutorial.

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   ...
   // Google Accounts using OAuth2
   m_credential = GoogleAccountCredential.usingOAuth2(this, Collections.singleton(DriveScopes.DRIVE));

   m_client = new com.google.api.services.drive.Drive.Builder(
            m_transport, m_jsonFactory, m_credential).setApplicationName("AppName/1.0")
            .build();
   ...
}

You can also check on these related issues:

Hope this helps!

widavies
  • 774
  • 2
  • 9
  • 22
0

There are a few things you are not doing. Firstly, you are losing a lot of information about why the connection is not successful because you are no inspecting the cause of the connection failure coming in the GoogleSignInResult parameter...

private void handleSignInResult(GoogleSignInResult result) {
}

Reading status codes

That parameter has useful information you are ignoring. You can retrieve the error status code if the sign-in is not successful...

private void handleSignInResult(GoogleSignInResult result) {
    if(!result.isSuccess()){

        Status status = result.getStatus();
    }
}

With that status, you can check why the sign-in failed by checking the status code

int code = status.getStatusCode();

if(code == GoogleSignInStatusCodes.SIGN_IN_REQUIRED){
}
else if(code == GoogleSignInStatusCodes.NETWORK_ERROR){
}

All the constants can be found in the GoogleSignInStatusCode documentation here...

https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInStatusCodes

and here...

https://developers.google.com/android/reference/com/google/android/gms/common/api/CommonStatusCodes

Resolving the sign-in issue

You could even attempt to resolve the problem by tdetermining if the error has resolution...

private void handleSignInResult(GoogleSignInResult result) {
    if(!result.isSuccess()){

        Status status = result.getStatus();

        if(status.hasResolution()){
            status.startResolutionForActivity(this, SIGN_IN_RESOLUTION_REQUEST_CODE);
        }
    }
}

which will start an intent that could probably resolve the issue for the user. Then you can check the result of that resolution on the onActivityResult method...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == SIGN_IN_RESOLUTION_REQUEST_CODE){
    }
}

Google API Availability

If all the above fails. The next thing you can do is check for Google API availability on the user's device. Basically, in one comment you admitted that your solution is INDEED working in SOME devices. If that is the case, then I'm inclined to point out that some Google Libraries require a specific version of Google Play services

The best way to know what exactly the problem is...it's by doing a Google API availability check and then output as much info as you can to logcat...you can do that easily, see below...

private boolean checkPlayServices(){
    GoogleApiAvailability gaa = GoogleApiAvailability.getInstance();
    int result = gaa.isGooglePlayServicesAvailable(getApplicationContext());
    Log.d("isGooglePlayServicesAvailable returned %d", result);

    if(result != ConnectionResult.SUCCESS){
        if(gaa.isUserResolvableError(result)){
            gaa.getErrorDialog(this,result, REQUEST_PLAY_SERVICES_RESOLUTION).show();
        }

        return false;
    }

    return true;
}

isGooglePlayServicesAvailable will return an integer constant that can be translated to one of the ConnectionResult constants. The list of constants and it's meaning is quite large to cover here.

Another key part of the code snippet above is that you could attempt to have the system resolve the error for you if the error is easily resolvable, for example, if the user needs to update Google Play Services on their device because the current version is outdated

Where would you use the above method?

Anywhere make the first connection attempt to Google APIs. For example, using your original code snippet...

private void handleSignInResult(GoogleSignInResult result) {

    if (result.isSuccess()) {
        //code block omitted
    } else {
        checkPlayServices();
    }
}
Leo
  • 14,625
  • 2
  • 37
  • 55
0

Google Signup OR Social Signup.

After complete Code side must do other things from Google and Firebase Console.

  1. Create SHA-1 Key from your pc.

    - from Right side of Android studio find Gradle option -> app  -> Signing Report. You will get SHA-1 key in log area when it done. (Watch in pic.)
    

enter image description here

 - add this key in Google firebase Console in App module

enter image description here

 -  after this module, download ***'google-services.json'*** and paste it 

    into project -> app -> :here paste that file: ex:/ D:\Thor_PC\Androidroject\android-customer_new\app

enter image description here

 -  and at the last option must check **Google Service is Enable or not** in Console.

 - Go to firebase console -> Engage (left side)  -> Authentication -> Sign-in Method.

enter image description here

IF AGAIN NOT ANY DATA INFECT ON LOGIN THEN REMOVE THE LINE FROM LOGIN CODE WITH USE OF ALL ABOVE STEP.

@Override
     public void initializeFBGoogle() {
     FacebookSdk.sdkInitialize(activity.getApplicationContext());
     GoogleSignInOptions googleSignInOptions = new 
     GoogleSignInOptions.Builder
     (GoogleSignInOptions.DEFAULT_SIGN_IN)
     //  .requestIdToken(REQUEST_ID_TOKEN)        (****REMOVE THE LINE****)
       .requestEmail()
       .build();
     mGoogleApiClient = new GoogleApiClient.Builder(activity)
    .enableAutoManage(activity, this)
    .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
    .build();
      }

☻♥ Done Keep Code.