Android Google登录出现ApiException "12500"
创始人
2024-08-14 00:00:19
0

当使用Google登录时,遇到ApiException "12500"错误通常是由于以下原因之一引起的:

  1. 未正确配置SHA-1指纹:在Google开发者控制台中,您需要为您的应用程序配置正确的SHA-1指纹。确保您在开发期间使用的密钥存储与配置的SHA-1指纹相匹配。您可以使用以下代码获取您的应用程序的SHA-1指纹:
try {
    PackageInfo info = getPackageManager().getPackageInfo(
            "com.example.packageName",
            PackageManager.GET_SIGNATURES);
    for (Signature signature : info.signatures) {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
    }
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}

在上述代码中,将“com.example.packageName”替换为您的应用程序的包名。在运行代码后,您将在日志中看到SHA-1指纹。

  1. 未启用Google登录API:您需要在Google开发者控制台中启用Google登录API。确保您的项目设置中已启用Google登录API。

  2. 未提供正确的OAuth 2.0客户端ID:在Google开发者控制台中,您需要为您的应用程序创建OAuth 2.0客户端ID,并在应用程序中使用正确的客户端ID。确保您在调用Google登录API时使用了正确的客户端ID。

以下是使用Firebase身份验证的示例代码,用于处理ApiException "12500"错误:

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;

// 在Activity或Fragment中的方法中调用以下代码:
private static final int RC_SIGN_IN = 9001;
private FirebaseAuth mAuth;
private GoogleApiClient mGoogleApiClient;

private void signIn() {
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    // 连接失败处理
                }
            })
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

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

    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);
        } else {
            Log.e(TAG, "Google Sign-In failed: " + result.getStatus());
        }
    }
}

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    if (task.isSuccessful()) {
                        FirebaseUser user = mAuth.getCurrentUser();
                        // 登录成功处理
                    } else {
                        Log.e(TAG, "Firebase authentication failed: ", task.getException());
                        // 登录失败处理
                    }
                }
            });
}

在上述代码中,R.string.default_web_client_id应替换为您的OAuth 2.0客户端ID。

希望这些解决方法能帮助您解决ApiException "12500"错误。如果问题仍然存在,请进一步检查配置并确保您的应用程序

相关内容

热门资讯

总结透视!wepoker怎么开... 总结透视!wepoker怎么开辅助,微乐脚本,要领教程(有挂猫腻)-哔哩哔哩1、这是跨平台的wepo...
总结透视!wepoker透视底... 总结透视!wepoker透视底牌,微乐脚本辅助器,教程书教程(有挂存在)-哔哩哔哩1、起透看视 we...
辅助透视!wepoker透视方... 辅助透视!wepoker透视方法,微乐家乡游戏攻略,指南教程(果真有挂)-哔哩哔哩所有人都在同一条线...
了解透视!hhpoker底牌透... 了解透视!hhpoker底牌透视脚本,微乐家乡app下载,要领教程(有挂方法)-哔哩哔哩1、玩家可以...
专业透视!扑克之星辅助,微乐麻... 专业透视!扑克之星辅助,微乐麻辣自建房辅助视频,妙计教程(的确有挂)-哔哩哔哩1、实时扑克之星辅助透...
了解透视!hhpoker怎么开... 了解透视!hhpoker怎么开透视,微乐游戏辅助脚本安卓,方式教程(有挂讲解)-哔哩哔哩1、hhpo...
科普透视!wpk辅助哪里买,微... 科普透视!wpk辅助哪里买,微乐家乡官方app,技法教程(有挂教程)-哔哩哔哩1.wpk辅助哪里买 ...
分享透视!德州局透视,微信微乐... 分享透视!德州局透视,微信微乐游戏有辅助吗,练习教程(确实有挂)-哔哩哔哩1、下载好德州局透视正确养...
揭露透视!we-poker软件... 揭露透视!we-poker软件,陕西微乐小程序脚本,妙计教程(发现有挂)-哔哩哔哩we-poker软...
揭露透视!hhpoker软件可... 揭露透视!hhpoker软件可以玩吗,微乐小程序辅助器,积累教程(有挂头条)-哔哩哔哩1、hhpok...