【发布时间】:2019-03-18 00:23:01
【问题描述】:
这是我的第一次尝试....试图找到过去 2 天的问题。 我正在尝试将 google 登录集成到 android 应用程序中,但是如下: com.google.android.gms.common.api.ApiException: 12500
遵循以下代码: https://firebase.google.com/docs/auth/android/google-signin
确保开发控制台中存在 oauth 客户端 ID,并按照其他帖子中的建议使用来自 ~/.android/debug.keystore 的正确 SHA-1 指纹。
在应用级别使用最新的播放服务 49 和 build.gradle:实现 'com.google.android.gms:play-services-auth:16.0.1'
在项目级 build.gradle 中使用以下代码:
buildscript { // ... 依赖项 { // ... 类路径 'com.google.gms:google-services:4.1.0' // google-services 插件 } }
public class SignUpActivity extends AppCompatActivity {
private GoogleSignInClient gsc;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
GoogleSignInOptions gso = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(String.valueOf(R.string.gplus_api_client_id))
.requestEmail()
.build();
gsc = GoogleSignIn.getClient(this, gso);
//Initialize firebase authentication
firebaseAuth = FirebaseAuth.getInstance();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task < GoogleSignInAccount > task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task < GoogleSignInAccount > completedTask) {
try {
//Sign in Successful
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
Log.w("SignUpActivity/handleSignInResult", "Trying signing in with Google... " + account);
firebaseAuthWithGoogle(account);
// Signed in successfully, show authenticated UI.
// Log.w("SignUpActivity/handleSignInResult", "Google sign in successful for account " + account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w("SignUpActivity/handleSignInResult", "Google sign in failed with exception: " + e);
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
Log.i("SignUpActivity/firebaseAuthWithGoogle", "Signed in as : " + account.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener < AuthResult > () {
@Override
public void onComplete(@NonNull Task < AuthResult > task) {
if (task.isSuccessful()) {
FirebaseUser user = firebaseAuth.getCurrentUser();
Log.i("SignUpActivity/firebaseAuthWithGoogle", "Sign in successful for user : " + user);
} else {
Log.e("SignUpActivity/firebaseAuthWithGoogle", "User Authentication failed.");
Snackbar.make(findViewById(R.id.view_signup), "Authentication failed.", Snackbar.LENGTH_SHORT);
}
}
});
}
}
【问题讨论】:
-
昨天已经关注这些...确保在 build.gradle 中更新了当前版本的 auth api 和 firebase,使用正确的 sha1 密钥设置了 clientid
标签: android