2016-12-29 33 views
2

我試圖用firebase實現谷歌登錄認證。 我正在關注this教程。java.lang.IllegalArgumentException:必須指定一個idToken或一個accessToken

錯誤日誌:

了java.lang.RuntimeException:失敗傳送結果 ResultInfo {誰= NULL,請求= 1002,結果= -1,數據=意向{(具有 額外)}},以活動 {com.clabs.codefosterapp/com.clabs.codefosterapp.SplashActivity}: java.lang.IllegalArgumentException:必須指定一個idToken或一個 accessToken。

在android.app.ActivityThread.deliverResults(ActivityThread.java:3389) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3432) 在android.app.ActivityThread.access $ 1300(ActivityThread.java :135.) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1244) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper。 java:136) at android.app.ActivityThread.main(ActivityThread.java:5045) at java .lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java :779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) 引起:java.lang.IllegalArgumentException:必須指定一個idToken 或accessToken。 在com.google.firebase.auth.GoogleAuthCredential。(未知 源) 在com.google.firebase.auth.GoogleAuthProvider.getCredential(未知 源) 在 com.clabs.codefosterapp.SplashActivity.firebaseAuthWithGoogle(SplashActivity。的java:102) 在 com.clabs.codefosterapp.SplashActivity.onActivityResult(SplashActivity.java:91) 在android.app.Activity.dispatchActivityResult(Activity.java:5423) 在android.app.ActivityThread.deliverResults(ActivityThread .java:3385) at android.app.Activit yThread.handleSendResult(ActivityThread.java:3432) 在android.app.ActivityThread.access $ 1300(ActivityThread.java:135) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1244) 的機器人。 os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5045) at java.lang。方法.invokeNative(本地方法) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAn dArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 在達爾維克。system.NativeStart.main(本機方法)

在崩潰以下行

AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null); 

我的代碼:

private void googleSignIn() { 
     Intent intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
     startActivityForResult(intent, SIGN_IN); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == SIGN_IN) { 
      GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
      if (result.isSuccess()) { 
       GoogleSignInAccount account = result.getSignInAccount(); 
       firebaseAuthWithGoogle(account); 
      } else { 

       Toast.makeText(SplashActivity.this, "Oops! Something Went Wrong", Toast.LENGTH_SHORT).show(); 
      } 

     } 
    } 
private void firebaseAuthWithGoogle(GoogleSignInAccount account) { 

     AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null); 
     mAuth.signInWithCredential(credential) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         if (!task.isSuccessful()) { 
          Toast.makeText(SplashActivity.this, "Authentication Failed", Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 
    } 
+0

你添加的依賴呢?您最好在這裏遵循官方文檔:https://firebase.google.com/docs/auth/android/google-signin –

+0

是的,我添加了它們。 – Shubh

+0

請確保您已遵循官方文檔中提供的每一步......然後告訴我。 –

回答

18

我檢討我的整個代碼,我發現,我沒有在構建GoogleSignInOptions時設置requestIdToken。 正確的代碼是:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
        .requestIdToken(getString(R.string.default_web_client_id)) 
        .requestEmail() 
        .build(); 
+0

這是描述在這裏你可以找到web_client_id https://stackoverflow.com/questions/34283355/firebase-serverclientid-when-building-googlesigninoptions –

3

您需要在您的火力地堡儀表盤的應用得到Web客戶端ID,並將其粘貼在這裏。

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
       .requestIdToken("firebase_web_client_id_for_google") 
       .requestEmail() 
       .build(); 
相關問題