22

I am developing an Android application where I want to use the Google API. For that I have imported the google-play-service-lib project.

I am following this link to initialize GoogleApiClient object.

My code:

1) In the onCreate() method I am building the GoogleApiClient object:

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(Plus.API, null)
    .addScope(Plus.SCOPE_PLUS_LOGIN)
    .build();

2) In onStart(), I call mGoogleApiClient.connect().

3) My activity implements ConnectionCallbacks and OnConnectionFailedListener.

4) My onConnectionFailed() method looks like:

public void onConnectionFailed(ConnectionResult result) {
    //in dubug result looks like : ConnectionResult{
    //statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent
                    // {41f8ca70: android.os.BinderProxy@41f8ca10}}
     try {
     if(!mIntentInProgress && result.hasResolution())
     {
         mIntentInProgress=true;
        result.startResolutionForResult(mActivity, RC_SIGN_IN);
          }

    } catch (SendIntentException e) {
        e.printStackTrace();
    }
}

5) My onActivityResult() method contains:

if (requestCode == RC_SIGN_IN) {
   if (!mGoogleApiClient.isConnecting()) {
      mGoogleApiClient.connect();
   }
}

When I run my app I get a Toast that says that an internal error popped up. I did create the project in the Google console.

Daniel
  • 2,355
  • 9
  • 23
  • 30
DCoder
  • 3,486
  • 7
  • 36
  • 68

11 Answers11

23

I had the same problem.

From documentation: The client may choose to continue without using the API or it may call startResolutionForResult(Activity, int) to prompt the user to sign in.

So you should try to sign in by using startResolutionForResult() function

@Override
public void onConnectionFailed(ConnectionResult result) {
   if (result.hasResolution()) {
       try {
           // !!!
           result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
       } catch (SendIntentException e) {
           mPlusClient.connect();
       }
   }

   mConnectionResult = result;
}
user2642219
  • 231
  • 2
  • 2
  • 3
    If the sign in intent fails, why would you try to call mPlusClient.connect() inside the catch block? At that point, the user hasn't signed in so the call would necessarily fail again. –  Jul 15 '15 at 03:11
  • 1
    Doesn't work for me. After trying startResolutionForResult() my onConnected() calls and I receive the email But Person object is received null after trying to fetch this object. – Rahul Rastogi Aug 11 '15 at 06:24
  • 1
    what is REQUEST_CODE_RESOLVE_ERR? I get unresolved symbol on REQUEST_CODE_RESOLVE_ERR. – nav_jan May 01 '16 at 08:59
  • 3
    @nav_jan it's a Request Code that they made up themselves. You can set yours easily `private static int REQUEST_CODE_RESOLVE_ERR = 1000;` or to any positive number you want. And than use it to check whether is that the write call that returns in your onActivityResult(). – Recomer Jun 24 '16 at 21:51
8

Just follow google's instructionss

AND CAUTION

Since you are still in development mode, check if you have added your testing email address in the GAME DETAILS center before publishing the game.

Naskov
  • 4,121
  • 5
  • 38
  • 62
6

Check that you are signing the app with a keystore that is in the apk uploaded to Google Play Developer's Console (if testing, you can upload as alpha and publish while keeping it private).

If not this, it could be other things. Make sure your account's email address is listed in testers in the Account details page (on the settings menu with a gear icon), and the licensed response is set to LICENSED, NOT to RESPOND_NORMALLY

Michael Chinen
  • 17,737
  • 5
  • 33
  • 45
3

This error indicates that the user needs to authorize your app. There's a full workflow for this, following the tutorial at https://developers.google.com/+/mobile/android/getting-started look for "onConnectionFailed"

Hounshell
  • 5,321
  • 4
  • 34
  • 51
3

Drive api should be enabled from the console page.

challenger
  • 2,184
  • 1
  • 18
  • 25
3

Not sure if this is the best answer but it worked for me. I copied onConnectionFailed() from the BasicSamples TakeANumber MainActivity. It calls BaseGameUtils to resolve. Of course that implies you have the BaseGameUtils library included in your project and that's another can of worms. But maybe you can get by with the one method so I copied it below.

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
   logger.info( "onConnectionFailed() *play* : attempting to resolve");
    if (mResolvingConnectionFailure) {
       logger.info( "onConnectionFailed(): already resolving");
        return;
    }

    if (mSignInClicked || mAutoStartSignInFlow) {
        mAutoStartSignInFlow = false;
        mSignInClicked = false;
        mResolvingConnectionFailure = true;
        if (!BaseGameUtils.resolveConnectionFailure(this, mGoogleApiClient, connectionResult,
                RC_SIGN_IN, getString(R.string.signin_other_error))) {
            mResolvingConnectionFailure = false;
        }
    }
}

From Google BasicSamples BaseGameUtils.java:

 public static boolean resolveConnectionFailure(Activity activity,
                                               GoogleApiClient client, ConnectionResult result, int requestCode,
                                               String fallbackErrorMessage) {

    if (result.hasResolution()) {
        try {
            result.startResolutionForResult(activity, requestCode);
            return true;
        } catch (IntentSender.SendIntentException e) {
            // The intent was canceled before it was sent.  Return to the default
            // state and attempt to connect to get an updated ConnectionResult.
            client.connect();
            return false;
        }
    } else {
        // not resolvable... so show an error message
        int errorCode = result.getErrorCode();
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(errorCode,
                activity, requestCode);
        if (dialog != null) {
            dialog.show();
        } else {
            // no built-in dialog: show the fallback error message
            showAlert(activity, fallbackErrorMessage);
        }
        return false;
    }
}
user6627139
  • 406
  • 6
  • 16
2

if you are not using debug keys, push app to google play as alpha, add yourself as a tester and MOST important - follow the "Opt-in" link on Aplha page and CONFIRM that you are the tester.

Max
  • 146
  • 2
  • 9
1

I'd like to share my experience with this. My case was when using Google Play Services for Google Play Games. I was also getting the onConnectionFailed giving SIGN_IN_REQUIRED error. Finally I realize I had not "Published" my Game settings in the developer console. Not to be mistaken for publishing an "alpha" or "beta" version of your apk. I mean the actual Google Play Games "Game" you create and link to the game's APK.

Jonathan
  • 11
  • 2
1

I had Same problem and solved with this solution , I actually had very old version google play-services library so I updated It with latest google play-service library to compile 'com.google.android.gms:play-services:11.0.4' from my previous version library to support your android SDK and maintain compileSDKVersion and targetSDKVersion in gradle:app. add google drive enabled API_KEY in manifest.and try again

Anjali
  • 11
  • 3
1

In my case the problem was in sensitive scopes added and not verified:

https://console.developers.google.com/apis/credentials Credentials -> OAuth consent screen -> Scopes for Google APIs

Make sure you have no unverified scopes.

Kirill Kuzyk
  • 99
  • 1
  • 7
0

for my cause i wrongly used release SHA1 key to generate API in developer console. Then created SHA1 key with debug.keystore and updated in my api credentials. Its started working.

keytool -list -v -keystore "C:\Users\<user>n\.android\debug.keystore" -alias androiddebugkey -storepass andro
id -keypass android
Anand saga
  • 1,433
  • 11
  • 23