0

我目前正在將我舊的Google Plus登錄代碼轉換爲新的方式。 我覺得很難理解基本原理。它不會工作。谷歌登錄Firebase無效Idp響應

第一步是說明如何得到簽名在工作: https://developers.google.com/identity/sign-in/android/sign-in

第二步是火力地堡。出於某種原因,您需要將以上文檔與以下內容結合使用: 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 

         } 
        }); 

     } 

這裏我在調用GoogleAuthPr時遇到異常使用getIdToken的ovider.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 - > google - > Web SDK配置下檢查了Firebase控制檯。 Webclient-ID和密碼與Google控制檯Web應用程序客戶端ID中的相匹配。

有誰知道如何解決這個問題?

回答

0

確定花了幾個小時後,我決定擺脫所有與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); 
} 

add onActivityResult callback。爲完成回調

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 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 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); 
        } 
}