Android Google+ login doesn't work - statusCod

2019-01-25 22:19发布

I'm trying to let the user login with Google+. I have created the project in the Google developer console, I got the Client ID and I have installed the Google play services in Android Studio. (My running device has Android 4.4.2) When the app is running and I press the login button a toast message appears: "Internal Error"

The error in the Logcat is the following:

error ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{429ffe38: android.os.BinderProxy@429ffdd8}}

Notice that this error appears before that the user has pressed the button, in fact, through debugging, I figured out it comes from this line code:

Log.e(TAG, "error " + result.toString());

which is in the method:

public void onConnectionFailed(ConnectionResult result) {
    if (!mGoogleIntentInProgress) {
        /* Store the ConnectionResult so that we can use it later when the user clicks on the Google+ login button */
        mGoogleConnectionResult = result;

        if (mGoogleLoginClicked) {
            /* The user has already clicked login so we attempt to resolve all errors until the user is signed in,
             * or they cancel. */
            resolveSignInError();
        } else {
            Log.e(TAG, "error " + result.toString());
        }
    }
}

Here the other important methods:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    SignInButton googleLoginButton = (SignInButton) findViewById(R.id.login_with_google);
    googleLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mGoogleLoginClicked = true;
            if (!mGoogleApiClient.isConnecting()) {
                if (mGoogleConnectionResult != null) {
                    resolveSignInError();
                } else if (mGoogleApiClient.isConnected()) {
                    getGoogleOAuthTokenAndLogin();
                } else {
                /* connect API now */
                    Log.d(TAG, "Trying to connect to Google API");
                    mGoogleApiClient.connect();
                }
            }
        }

    });

    /* Setup the Google API object to allow Google+ logins */
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .build();
}

 private void resolveSignInError() {
    if (mGoogleConnectionResult.hasResolution()) {
        try {
            mGoogleIntentInProgress = true;
            mGoogleConnectionResult.startResolutionForResult(this, RC_GOOGLE_LOGIN);
        } 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.
            mGoogleIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }
}

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_GOOGLE_LOGIN) {
        /* This was a request by the Google API */
        if (resultCode != RESULT_OK) {
            mGoogleLoginClicked = false;
        }
        mGoogleIntentInProgress = false;
        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    }


}

Here the code in the AndroidManifest.xml

<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />

And the meta-data

   <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Hope you can help me, thanks a lot!

2条回答
一夜七次
2楼-- · 2019-01-25 23:04

Depending of your action, if you don't really need to be signed, you can keep the connection open.

Replace

mGoogleApiClient.connect();

with

mGoogleApiClient.connect(2); // SIGN_IN_MODE_OPTIONAL = 2
查看更多
Animai°情兽
3楼-- · 2019-01-25 23:11

As in my project, there are many problems with Google+ sign in. Example, when create mGoogleApiClient, you should

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

with SCOPE_PLUS_PROFILE - OAuth 2.0 scope for accessing the user's Google+ profile data.

You need to enable Google+ API and create Credentials with SHA1 and your package.

OAuth Consent screen with your email and product name (required)



More, make sure:

applicationId in tag <application>
package name in AndroidManifest.xml
package name in Credentials in Google Developer Console

all same


More, make sure you have the permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 

More, in method @Override protected void onActivityResult(int requestCode, int resultCode, Intent data { ... ... }

DO NOT need call super.onActivityResult(requestCode, resultCode, data);

And more thing also important: debug keystore and release keystore.

And more .....

查看更多
登录 后发表回答