无法通过 Google 客户端 API Android 进行身份验证
cannot authenticate by Google Client API Android
我正在尝试使用 Firebase 的 Android 应用。
我需要通过 Google 客户端 API 进行身份验证才能使用 Firebase。
但是,我无法通过 Google 客户端 API.
进行身份验证
在下面的代码中,GoogleSignInResult.isSuccess()
returns false。
GoogleSignInResult.getState().getStateMessage()
returns 空。所以,我不知道为什么Google认证失败。
使用 Google 身份验证时有什么必须做的吗?
提前致谢。
注意:
当我通过命令“添加安装release.apk”安装release.apk时,我可以通过Google客户端API进行身份验证。
但是当我通过 Google Play Store(不是 BETA 而是 RELEASE)安装时我无法进行身份验证。
截图:
按下登录按钮后(登录 button:lower 右键)
代码:
在 onActivityResult 中,GoogleSignInResult.isSuccess()
returns false.
public class LoginActivity extends AppCompatActivity {
private static final int REQUEST_CODE_SIGN_IN = 9001;
private FirebaseAuth firebaseAuth;
public static GoogleApiClient googleAPIClient;
private DatabaseReference usersRef;
private ProgressDialog progressDialog;
private GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(LoginActivity.this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("*** LoginActivity.onCreate - start");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
try {
progressDialog = new ProgressDialog(this);
GoogleSignInOptions options = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
googleAPIClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, onConnectionFailedListener)
.addApi(Auth.GOOGLE_SIGN_IN_API, options)
.build();
firebaseAuth = FirebaseAuth.getInstance();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("*** LoginActivity.onCreate - start");
}
@Override
public void onStop() {
System.out.println("LoginActivity.onStop - start");
if (null != usersRef) {
usersRef.removeEventListener(valueEventlistener);
}
super.onStop();
}
public void onCancelButtonClick(View view) {
finish();
}
public void onLoginButtonClick(View view) {
System.out.println("*** LoginActivity.onLoginButtonClick - start");
Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleAPIClient);
startActivityForResult(intent, REQUEST_CODE_SIGN_IN);
System.out.println("*** LoginActivity.onLoginButtonClick - end");
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("*** LoginActivity.onActivityResult - start");
if (requestCode == REQUEST_CODE_SIGN_IN) {
System.out.println("*** LoginActivity.onActivityResult - in if");
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
System.out.println("*** LoginActivity.onActivityResult - in if success");
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
System.out.println("*** LoginActivity.onActivityResult - in if error");
System.out.println("*** LoginActivity.onActivityResult - in if error state:" + result.getStatus().getStatusMessage());
Toast.makeText(LoginActivity.this, "Error:" + result.getStatus().getStatusMessage(), Toast.LENGTH_SHORT).show();
}
}
System.out.println("*** LoginActivity.onActivityResult - end");
}
public void firebaseAuthWithGoogle(GoogleSignInAccount account) {
// firebase authentication
}
您是否在 Firebase 项目设置中添加了您的发布版本 SHA1 指纹?
我正在尝试使用 Firebase 的 Android 应用。 我需要通过 Google 客户端 API 进行身份验证才能使用 Firebase。 但是,我无法通过 Google 客户端 API.
进行身份验证在下面的代码中,GoogleSignInResult.isSuccess()
returns false。
GoogleSignInResult.getState().getStateMessage()
returns 空。所以,我不知道为什么Google认证失败。
使用 Google 身份验证时有什么必须做的吗? 提前致谢。
注意: 当我通过命令“添加安装release.apk”安装release.apk时,我可以通过Google客户端API进行身份验证。 但是当我通过 Google Play Store(不是 BETA 而是 RELEASE)安装时我无法进行身份验证。
截图:
按下登录按钮后(登录 button:lower 右键)
代码:
在 onActivityResult 中,GoogleSignInResult.isSuccess()
returns false.
public class LoginActivity extends AppCompatActivity {
private static final int REQUEST_CODE_SIGN_IN = 9001;
private FirebaseAuth firebaseAuth;
public static GoogleApiClient googleAPIClient;
private DatabaseReference usersRef;
private ProgressDialog progressDialog;
private GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(LoginActivity.this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("*** LoginActivity.onCreate - start");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
try {
progressDialog = new ProgressDialog(this);
GoogleSignInOptions options = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
googleAPIClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, onConnectionFailedListener)
.addApi(Auth.GOOGLE_SIGN_IN_API, options)
.build();
firebaseAuth = FirebaseAuth.getInstance();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("*** LoginActivity.onCreate - start");
}
@Override
public void onStop() {
System.out.println("LoginActivity.onStop - start");
if (null != usersRef) {
usersRef.removeEventListener(valueEventlistener);
}
super.onStop();
}
public void onCancelButtonClick(View view) {
finish();
}
public void onLoginButtonClick(View view) {
System.out.println("*** LoginActivity.onLoginButtonClick - start");
Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleAPIClient);
startActivityForResult(intent, REQUEST_CODE_SIGN_IN);
System.out.println("*** LoginActivity.onLoginButtonClick - end");
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("*** LoginActivity.onActivityResult - start");
if (requestCode == REQUEST_CODE_SIGN_IN) {
System.out.println("*** LoginActivity.onActivityResult - in if");
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
System.out.println("*** LoginActivity.onActivityResult - in if success");
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
System.out.println("*** LoginActivity.onActivityResult - in if error");
System.out.println("*** LoginActivity.onActivityResult - in if error state:" + result.getStatus().getStatusMessage());
Toast.makeText(LoginActivity.this, "Error:" + result.getStatus().getStatusMessage(), Toast.LENGTH_SHORT).show();
}
}
System.out.println("*** LoginActivity.onActivityResult - end");
}
public void firebaseAuthWithGoogle(GoogleSignInAccount account) {
// firebase authentication
}
您是否在 Firebase 项目设置中添加了您的发布版本 SHA1 指纹?