Google 尝试使用 Google+ 登录时 Play 服务不可用

Google Play Services unavailable when trying to Log In with Google+

我正在尝试在我的应用程序中实现 Google+ 登录,但它不起作用。

每次我点击登录时,一旦我选择了账户,onConnectionFailed 就会被调用。

有人可以告诉我哪里出了问题吗?

public class LoginActivity extends ActionBarActivity
    implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener{

/*
Variables
 */

/* Request code used to invoke sign in user interactions. */
private static final int RC_SIGN_IN = 0;

/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;

/* A flag indicating that a PendingIntent is in progress and prevents
 * us from starting further intents.
 */
private boolean mIntentInProgress;

/*
 * True if the sign-in button was clicked.  When true, we know to resolve all
 * issues preventing sign-in without waiting.
 */
private boolean mSignInClicked;

/*
Lifecycle
 */

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

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

    // Sign in button click listener
    findViewById(R.id.googleSignInButton).setOnClickListener(this);
}

@Override
protected void onStart() {
    super.onStart();

    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

/*
Callbacks
 */

@Override
public void onClick(View v) {
    if (v.getId() == R.id.googleSignInButton && !mGoogleApiClient.isConnecting()) {
        mSignInClicked = true;
        mGoogleApiClient.connect();
    }
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i("TAG", "onConnectionFailed");
    if (!mIntentInProgress) {
        if (mSignInClicked && connectionResult.hasResolution()) {
            // The user has already clicked 'sign-in' so we attempt to resolve all
            // errors until the user is signed in, or they cancel.
            try {
                connectionResult.startResolutionForResult(this, RC_SIGN_IN);
                mIntentInProgress = 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.
                mIntentInProgress = false;
                mGoogleApiClient.connect();
            }
        }
    }
}

@Override
public void onConnected(Bundle bundle) {
    Log.i("TAG", "onConnected");

    mSignInClicked = false;
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionSuspended(int i) {
    Log.i("TAG", "onConnectionSuspended");
    mGoogleApiClient.connect();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN) {
        if (resultCode != RESULT_OK) {
            mSignInClicked = false;
        }

        mIntentInProgress = false;

        if (!mGoogleApiClient.isConnected()) {
            mGoogleApiClient.reconnect();
        }
    }
}
}

我什至下载了官方 Google 登录示例,但我得到了同样的错误。它只是不会登录和连接。

我的网络连接很好(甚至是 wifi),我在多部手机上都试过了。

这可能只是您使用的密钥有问题。 转到您的 google api 的控制台尝试生成一个新的 Cient id 从 debug.keystore 获得的 SHA1 并尝试登录 again.I相信它会帮助解决您的问题。