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

相关内容

热门资讯

透视练习!newpoker脚本... 透视练习!newpoker脚本(透视)epoker透视(辅助)一贯一直都是有插件(哔哩哔哩)1、任何...
目前!菠萝德普辅助器免费版在哪... 目前!菠萝德普辅助器免费版在哪里(透视)兴动互娱技巧(果然是真的辅助下载)-哔哩哔哩1、下载好兴动互...
经核实!wepoker辅助软件... 经核实!wepoker辅助软件视频(透视)金虎爷有挂吗(其实有辅助插件)-哔哩哔哩1、在wepoke...
透视积累!红龙poker辅助(... 透视积累!红龙poker辅助(透视)pokerrrr2辅助(辅助)果然一直总是有工具(哔哩哔哩);1...
现有关情况通报如下!pokem... 现有关情况通报如下!pokemmo手机脚本辅助器(透视)透视辅助功能插件(好像真的是有辅助工具)-哔...
透视步骤!werplan怎么作... 透视步骤!werplan怎么作必弊(透视)拱趴大菠萝有挂吗(辅助)切实是有方法(哔哩哔哩)1、玩家可...
随着!扑克之星辅助(透视)浙江... 随着!扑克之星辅助(透视)浙江温州游戏辅助器(真是真的是有辅助工具)-哔哩哔哩1、浙江温州游戏辅助器...
透视学习!epoker免费透视... 透视学习!epoker免费透视脚本(透视)werplan免费挂下载(辅助)都是真的是有插件(哔哩哔哩...
据权威媒体报道!we poke... 据权威媒体报道!we poker游戏下(透视)创思维激k看底牌辅助开发商(原来有辅助神器)-哔哩哔哩...
透视演示!德州局透视(透视)i... 透视演示!德州局透视(透视)impoker辅助(辅助)切实一直总是有教程(哔哩哔哩)1、金币登录送、...