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

相关内容

热门资讯

透视讲解!wpk私人局有透视吗... 您好,wepoker透视辅助下载这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
透视app!红龙poker有辅... 透视app!红龙poker有辅助吗(透视)开挂辅助器(果然存在有挂)-哔哩哔哩所有人都在同一条线上,...
透视辅助!epoker透视底牌... 透视辅助!epoker透视底牌,哈糖大菠萝开挂,阶段教程(总是是有挂)-哔哩哔哩1、玩家可以在哈糖大...
透视插件!wepoker辅助器... 透视插件!wepoker辅助器激活码(透视)开挂辅助器(其实有挂)-哔哩哔哩1)wepoker辅助器...
透视教程!hhpoker德州挂... 透视教程!hhpoker德州挂真的有吗,werplan透视挂,学习教程(一贯存在有挂)-哔哩哔哩1、...
透视游戏!aapoker免费透... 透视游戏!aapoker免费透视脚本,xpoker辅助工具,诀窍教程(一贯存在有挂)-哔哩哔哩1)a...
透视大厅!aa poker透视... 透视大厅!aa poker透视软件(透视)开挂辅助器(原来是有挂)-哔哩哔哩1、超多福利:超高返利,...
透视脚本!xpoker辅助工具... 透视脚本!xpoker辅助工具,pokernow辅助工具,方案教程(一贯真的有挂)-哔哩哔哩1、不需...
透视修改器!wepoker透视... 透视修改器!wepoker透视有吗(透视)开挂辅助神器(竟然是有挂)-哔哩哔哩wepoker透视有吗...
透视科技!wejoker透视方... 透视科技!wejoker透视方法,wepoker线上大神,操作教程(确实存在有挂)-哔哩哔哩在进入w...