Why Does Google+ login accomplish login with an Er

2019-07-07 19:26发布

I've been dealing with Google+ login on android lately, and one thing keeps bugging me.

In all of their officially sanctioned examples, there isn't a method that specifically shows the Login process. The method that is called every time you try to log someone in is called resolveSignInError() shown here:

private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
            Log.d("mGoogleApiClient ", mGoogleApiClient.toString());

        } catch (IntentSender.SendIntentException e) {
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }
}

This method also relies on the fact that there is a mConnectionResult, which is set in the on connection failure method

public void onConnectionFailed(ConnectionResult result) {
    if (!result.hasResolution()) {
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                0).show();
        return;
    }

    if (!mIntentInProgress) {
        // Store the ConnectionResult for later usage
        mConnectionResult = result;

        if (mSignInClicked) {
            // The user has already clicked 'sign-in' so we attempt to
            // resolve all
            // errors until the user is signed in, or they cancel.
            resolveSignInError();
        }
    }
} 

So the entire scheme revolves around having already failed to login at least once. Does anyone know why there isn't a method that directly logs you into google plus? Why does this make sense as a way to structure readable software?

3条回答
【Aperson】
2楼-- · 2019-07-07 19:46

The reason is that that the device can't know whether you actually need to log in or not - sign in is really more like accessing a facility (as you might with location) than it is entering a username and password. The basic flow is:

  1. Connect to Google Play services, see if the app has been authorised.
  2. If it has, the connection succeeds.
  3. If not, the connection fails.

If you then want to log in, you go through the connection result resolution.

The subtle thing is that you may go to step 2 even if the user has never used the app on the device before! So, for example, if I go to your website and sign in there, then install your android app.

  1. The app starts, and retrieves a list of your user accounts (via the GET_ACCOUNTS permission)
  2. It tries to connect to Google Play services with any of the accounts
  3. Google Play services calls out to the Google auth servers, finds out you have already signed in to that application (in this case, on the web)
  4. The app gets the onConnected callback

So the reason that there is no login method is that the login status is actually remote - it lives on the Google servers, not on the device itself. The flow therefore has to be asynchronous.

查看更多
放我归山
3楼-- · 2019-07-07 19:51

Here is pretty normal example https://developers.google.com/+/mobile/android/sign-in:

There is method

`@Override
public void onConnected(Bundle connectionHint) {
  mSignInClicked = false;
  Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
}`

first steps are there https://developers.google.com/+/mobile/android/getting-started

查看更多
Ridiculous、
4楼-- · 2019-07-07 20:08

I am aware that this thread is very old. Updating as this comes up every time on Google. Now that google play store API v 8.4 is released, onConnectionFailed does exactly what it has to do.

Check this link https://github.com/googlesamples/google-services/blob/master/android/signin/app/src/main/java/com/google/samples/quickstart/signin/SignInActivity.java

查看更多
登录 后发表回答