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

相关内容

热门资讯

透视最新"wepok... 透视最新"wepoker怎么发冤家牌"竞技联盟透视插件(确实存在有辅助app)-哔哩哔哩亲,关键说明...
针对"hhpoker... 针对"hhpoker透视脚本下载"wepoker有机器人吗(好像存在有辅助工具)-哔哩哔哩1、超多福...
现有说明如下"wep... 现有说明如下"wepoker辅助透视软件"hhpoker辅助器视频(好像有辅助下载)-哔哩哔哩1、操...
透视神器"hhpok... 透视神器"hhpoker是内部控制吗"newpoker怎么安装脚本(都是真的是有辅助工具)-哔哩哔哩...
透视数据"wepok... 透视数据"wepoker透视有用吗"wepoker私人局辅助(确实存在有辅助安装)-哔哩哔哩1、进入...
目前来看"哈糖大菠萝... 目前来看"哈糖大菠萝挂法"hhpoker真能买到挂吗(总是有辅助辅助器)-哔哩哔哩进入游戏-大厅左侧...
长期以来"菠萝辅助器... 长期以来"菠萝辅助器免费版的功能介绍"wepoker破解工具(真是是真的辅助辅助器)-哔哩哔哩1、金...
随着"wepoker... 随着"wepoker正确养号方法"聚星ai辅助工具激活码(好像是真的辅助app)-哔哩哔哩1、让任何...
备受关注的"we-p... 备受关注的"we-poker靠谱吗"pokemmo脚本最新版(果然真的是有辅助软件)-哔哩哔哩1、p...
据报道"aapoke... 据报道"aapoker辅助器是真的吗"HH平台挂(好像真的有辅助辅助器)-哔哩哔哩1、下载好aapo...