android - 即使在提供准确的客户端 ID 后仍获取 'E/TokenRequestor: You have wrong OAuth2 related configurations. Detailed error: INVALID_AUDIENCE'

标签 android firebase google-api-client firebase-authentication google-signin

我在我的应用中提供了 GoogleSignIn 选项。

这是我的代码:

public class SignupActivity extends AppCompatActivity {

    private static final int RC_SIGN_IN = 1;
    GoogleApiClient mGoogleApiClient;
    FirebaseAuth mAuth;
    TextView appName;
    ProgressDialog signinProgressDialog;
    CoordinatorLayout coordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);

        signinProgressDialog = new ProgressDialog(SignupActivity.this);

        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.signupCoordinatorLayout);

        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.o_auth_client_id))
                .requestEmail()
                .requestProfile()
                .requestId()
                .build();

        // Build a GoogleApiClient with access to the Google Sign-In API and the
        // options specified by gso.
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

                    }
                } /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        mAuth = FirebaseAuth.getInstance();

        findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isNetworkAvailable()) {
                    signinProgressDialog.setMessage("Signing in...");
                    signinProgressDialog.show();
                    signIn();
                } else {
                    Snackbar snackbar = Snackbar
                            .make(coordinatorLayout, "No internet connection!", Snackbar.LENGTH_LONG);
                    snackbar.show();
                    signinProgressDialog.dismiss();
                }
            }
        });

    }

    private void signIn() {
        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);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = result.getSignInAccount();
                firebaseAuthWithGoogle(account);
            } else {
                // Google Sign In failed, update UI appropriately
                // ...
                Snackbar snackbar = Snackbar
                        .make(coordinatorLayout, "Error signing in!", Snackbar.LENGTH_LONG);
                snackbar.show();
                signinProgressDialog.dismiss();
            }
        }
    }

    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        Log.d("firebaseAuthWithGoogle", "firebaseAuthWithGoogle:" + acct.getId());

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d("signin_successful", "signInWithCredential:onComplete:" + task.isSuccessful());

                        Intent mainActivityIntent = new Intent(SignupActivity.this, MainActivity.class);
                        mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(mainActivityIntent);
                        signinProgressDialog.dismiss();

                        // If sign in fails, display a message to the user. If sign in succeeds
                        // the auth state listener will be notified and logic to handle the
                        // signed in user can be handled in the listener.
                        if (!task.isSuccessful()) {
                            Log.w("signin_unsuccessful", "signInWithCredential", task.getException());
                            Toast.makeText(SignupActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                            signinProgressDialog.dismiss();
                        }
                        // ...
                    }
                });
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) SignupActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}

我有 2 个客户端 ID,一个是 Web 客户端,另一个是 Android 客户端。

我已经用我的 google-services.json 文件和我在 console.developers.google.com 上的项目检查了我的客户端 ID,它们都匹配。

但我仍然收到此错误:E/TokenRequestor: 您的 OAuth2 相关配置有误,请检查。详细错误:INVALID_AUDIENCE 06-27 12:40:14.651 4443-4484/? D/AuthAccountOperation:id token 请求失败。

什么可能导致此错误?

是不是我的应用程序不再连接到我的控制台上的应用程序,还是其他原因?

请告诉我。

最佳答案

您需要在项目设置中为每个 Android 应用程序添加 SHA 指纹。如果您使用的是 Firebase,请使用您的项目名称编辑以下链接,然后为您的应用添加指纹。

https://console.firebase.google.com/project/{YOUR_PROJECT_NAME}/settings/general/ 

关于android - 即使在提供准确的客户端 ID 后仍获取 'E/TokenRequestor: You have wrong OAuth2 related configurations. Detailed error: INVALID_AUDIENCE',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38047998/

相关文章:

google-calendar-api - Google Calendar API - 具有最大时间限制的重复事件

php - 使用访问 token 获取 Google 客户端

Angular 火力错误

android - picasso :2个 fragment 之间的共享元素过渡

java - 重命名一个项目应该这么难吗?

android - 当我尝试将更改监听器添加到搜索栏时,应用程序关闭(Android Studio,Kotlin)

java - 如何从火力数据库中获取数据

javascript - 在 firestore 中查找具有 id 的特定字段

python-3.x - 如何模拟 googleapiclient.discovery.build 以单元测试从谷歌表格读取

android - 当 "am start" Activity 时, "total time"是什么意思?