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"错误。如果问题仍然存在,请进一步检查配置并确保您的应用程序

相关内容

热门资讯

透视神器!pokemmo手机版... 透视神器!pokemmo手机版修改器,淘宝的微乐辅助是真的吗,解密脚本(有挂猫腻)-哔哩哔哩在进入p...
脚本解迷!德扑HHpoker有... 脚本解迷!德扑HHpoker有挂吗,hhpoker免费辅助器(透视)竟然是有攻略(哔哩哔哩)1)辅助...
透视透视挂!we poker游... 透视透视挂!we poker游戏下,微乐家乡小程序辅助,关于脚本(有人有挂)-哔哩哔哩we poke...
脚本曝光!wepoker透视底... 脚本曝光!wepoker透视底牌脚本,wejoker开挂(透视)都是真的是有插件(哔哩哔哩)1、玩家...
更值得关注的是!wepoker... 您好,wepoker透视脚本网页这款游戏可以开挂的,确实是有挂的,需要了解加去威信【48527505...
脚本有挂!wepoker破解是... 脚本有挂!wepoker破解是真的还是假的,wepoker破解器(透视)竟然是有器(哔哩哔哩)1、模...
透视有挂!pokemmo辅助脚... 透视有挂!pokemmo辅助脚本,微信小程序微乐房间的挂多少钱,详情透视(有挂辅助)-哔哩哔哩pok...
透视必备!wepoker透视底... 透视必备!wepoker透视底牌脚本,wepoker私人辅助器(透视)其实存在有器(哔哩哔哩)1、许...
今年以来!大菠萝免费辅助器,微... 今年以来!大菠萝免费辅助器,微信小程序微乐辅助器,科普脚本(有挂功能)-哔哩哔哩大菠萝免费辅助器透视...
透视安装!newpoker脚本... 透视安装!newpoker脚本,手机微乐小程序游戏破解器,分享透视(有挂猫腻)-哔哩哔哩newpok...