Google登录使用Firebase无效的错误响应

时间:2016-11-27 23:31:58

标签: android exception firebase oauth-2.0 firebase-authentication

我目前正在将旧的Google Plus登录代码转换为新方式。 我发现很难理解基本原理。它不会工作。

第一步是解释如何登录工作: https://developers.google.com/identity/sign-in/android/sign-in

第二步是Firebase。出于某种原因,您需要将以上文档与以下内容结合起来: https://firebase.google.com/docs/auth/android/google-signin

为什么会这样?它的目的似乎过于复杂:只需登录即可。 无论如何。您必须执行以下操作:

在您的活动的onCreate中,您需要添加:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(webClientId)
                .build();

        mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
                .enableAutoManage(mContext /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        mAuth = FirebaseAuth.getInstance();

在您的登录点击处理程序中添加

Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
mActivity.startActivityForResult(signInIntent, RC_SIGN_IN);

onActivityResult中你必须添加

if (requestCode == RC_SIGN_IN) {
     GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

            AuthCredential credential = GoogleAuthProvider.getCredential(result.getSignInAccount().getIdToken(), null);
            mAuth.signInWithCredential(credential)
                    .addOnCompleteListener(mActivity, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            //do something

                        }
                    });

        }

在使用getIdToken调用GoogleAuthProvider.getCredential时出现异常。

  

com.google.firebase.FirebaseException:发生内部错误。 [无效的Idp响应:不允许Google id_token与此应用程序一起使用。其受众(OAuth 2.0客户端ID)为xxxxxxxxxxxx-xxxxxxxxxxxxxx.apps.googleusercontent.com,未授权在project_number:xxxxxxxxxxxx的项目中使用。

只需用

打印出令牌
  。

result.getSignInAccount()getIdToken();   工作正常。

我几乎可以肯定这个web_client_id是正确的,因为当我更换它时,初始登录请求将失败。

我在auth下检查了Firebase控制台 - &gt;谷歌 - &gt; Web SDK配置。 Webclient-ID和机密与Google控制台webapp客户机ID中的相匹配。

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

好好花了几个小时后,我决定摆脱所有与firebase相关的东西。现在它有效。不知道为什么你还需要那个firebase的东西。

登录时我有以下内容:

在您的活动或activityFragment中添加:

private static Activity mActivity;
private FragmentActivity mContext;
private GoogleApiClient mGoogleApiClient;
private GoogleSignInResult cachedSignInResult;
private String webClientId = "XXXXXXXXXXXX-xxxxxxxxxxxxxxx.apps.googleusercontent.com";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(webClientId)
                .build();

    mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
                .enableAutoManage(mContext /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
}

添加处理登录请求的逻辑。

public void requestLogin(OnLoginCompleteListener onLoginCompleteListener) {

    Log.d(TAG,"request login");

    //already logged in scenarios
    if (isConnected() && cachedSignInResult != null){
        Log.d(TAG,"you are already logged in with "+cachedSignInResult.getDisplayName());
        onLoginCompleteListener.onLoginSuccess()
        return;
    }
    else if (isConnected()){
        Log.d(TAG,"already connected, but no ");
        silentLogin(onLoginCompleteListener);
        return;
    }

    forceRegularLogin(onLoginCompleteListener);
}


public boolean isConnected() {
    if (mGoogleApiClient == null){
        Log.d(TAG,"cannot check isConnected without api client");
        return false;
    }
    return mGoogleApiClient.isConnecting() || mGoogleApiClient.isConnected();
}

private void silentLogin(final OnLoginCompleteListener onLoginCompleteListener){

    OptionalPendingResult<GoogleSignInResult> pendingResult =
            Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);

    pendingResult.setResultCallback(new ResultCallback<GoogleSignInResult>() {
        @Override
        public void onResult(@NonNull GoogleSignInResult   googleSignInResult) {

            if (googleSignInResult.isSuccess()){
                Log.d(TAG,"result is ok with name "+googleSignInResult.getSignInAccount().getDisplayName());
                cachedSignInResult = googleSignInResult;
                onLoginCompleteListener.onLoginSuccess()
            }
            else{
                Log.d(TAG,"not logged in silently. do regular login");
                requestLogin(onLoginCompleteListener);
            }
        }
    });
}


private void forceRegularLogin(OnLoginCompleteListener onLoginCompleteListener){
    if (mGoogleApiClient == null){
        Log.d(TAG,"cannot login in. no google api client");
        onLoginCompleteListener.error("no_google_api_client_found");
        return;
    }

    registeredLoginCallback = onLoginCompleteListener; //register for later use in activity result

    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    mActivity.startActivityForResult(signInIntent, RC_SIGN_IN);
}

添加onActivityResult回调。

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG,"on activity result");

    GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

    if (result.isSuccess()){

        if (registeredLoginCallback != null) {
            registeredLoginCallback.onLoginSuccess();
        }
        else{
            Log.e(TAG,"no callback registered for logging in result ok");
        }
    }
    else{
        if (registeredLoginCallback != null) {
            registeredLoginCallback.error();
        }
        else{
            Log.e(TAG,"no callback registered for logging in result error");
        }
    }
}

添加完整回调的界面

public interface OnLoginCompleteListener{
     public void onLoginSuccess();
     public void error(String message);
}

现在在您的登录按钮回调中添加

 requestLogin(new OnLoginCompleteListener() {
      @Override
      public void onLoginSuccess() {
          Log.d(TAG,"logged in");
      }
      @Override
      public void error(String message) {
          Log.d(TAG,"error logging in"+message);
      }
 }

此外,您可以添加以下方法和界面来获取用户个人资料信息

public void requestCurrentPerson(final OnRequestSocialPersonCompleteListener onRequestSocialPersonCompleteListener) {

    Log.d(TAG,"request current person");

    silentLogin(new OnLoginCompleteListener() {
        @Override
        public void onLoginSuccess(i) {

            SocialPerson socialPerson = new SocialPerson();
            socialPerson.id = signInResult.getSignInAccount().getId();
            socialPerson.name = signInResult.getSignInAccount().getDisplayName();
            socialPerson.avatarURL = signInResult.getSignInAccount().getPhotoUrl().toString();
            onRequestSocialPersonCompleteListener.onRequestSocialPersonSuccess(socialPerson);
        }

        @Override
        public void error(String message) {
            onRequestSocialPersonCompleteListener.error("error_fetching_social_person "+message);
        }
    });
}


public interface OnRequestSocialPersonCompleteListener {

    public void onRequestSocialPersonSuccess(SocialPerson socialPerson);
    public void error(String message);
}

登录后,您将致电

requestCurrentPerson(new OnRequestSocialPersonCompleteListener() {
                    @Override
                    public void onRequestSocialPersonSuccess( SocialPerson socialPerson) {
                            Log.d(TAG,"this is my persoin "+socialPerson);
                    }
                    @Override
                    public void error(Message message) {
                            Log.d(TAG,"error_fetching_social_person " +message);
                    }
}