我想让用户使用他的Google帐户登录我的应用。
我在google开发者控制台中设置了客户端ID,包含我的调试密钥库的软件包名称和sha1,启用了google plus api并填写了同意屏幕。
以下是使用googleApiClient
执行登录的活动代码:
public class IntroActivity extends BaseActivity implements
LoginFragment.FragmentListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final int RC_GOOGLE_SIGN_IN = 0;
private GoogleApiClient googleApiClient;
private boolean intentInProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
if (savedInstanceState == null) {
LoginFragment loginFragment = new LoginFragment();
replaceFragment(loginFragment, false);
}
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API, Plus.PlusOptions.builder().build())
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
@Override
protected void onStop() {
super.onStop();
if (googleApiClient.isConnected()) {
googleApiClient.disconnect();
}
}
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
switch (requestCode) {
case RC_GOOGLE_SIGN_IN:
intentInProgress = false;
if (!googleApiClient.isConnecting()) {
googleApiClient.connect();
}
break;
}
}
@Override
public void onGoogleSignInClick() {
googleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
if (Plus.PeopleApi.getCurrentPerson(googleApiClient) != null) {
String email = Plus.AccountApi.getAccountName(googleApiClient);
String token = null;
try {
token = GoogleAuthUtil.getToken(this, email, Plus.SCOPE_PLUS_LOGIN.toString());
} catch (IOException | GoogleAuthException e) {
e.printStackTrace();
}
showShortToast(token);
}
}
@Override
public void onConnectionSuspended(int i) {
googleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (!intentInProgress && connectionResult.hasResolution()) {
try {
intentInProgress = true;
startIntentSenderForResult(connectionResult.getResolution().getIntentSender(), RC_GOOGLE_SIGN_IN, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
intentInProgress = false;
googleApiClient.connect();
}
} else {
showShortToast("onConnectionFailed");
}
}
}
一旦googleApiClient
开始连接,它就无法连接,它会显示一个弹出窗口以便选择该帐户。一旦选中,它仍然采用onConnectionFailed
方法,但这次似乎没有解决问题的方法。
ConnectionResult
获取{statusCode=SIGN_IN_FAILED, resolution=null}
为什么我登录失败?应用程序是否可能以某种方式与开发控制台上的项目无关?