2016-12-15 43 views
0

我想要做的是使用Firebase在Facebook上登錄,然後獲取姓名,電子郵件,個人資料圖片和uid,然後將其存儲到Firebase數據庫。Firebase中的Facebook身份驗證不起作用

一切正常,直到點擊登錄按鈕,然後彈出Facebook帳戶窗口。之後,當我通過點擊「繼續使用Rishabh」選擇一個帳戶時,沒有任何反應。

沒有認證,沒有錯誤,沒有。同一個Facebook賬戶選擇窗口停留在屏幕上,沒有任何反應。

任何幫助將不勝感激。

這裏是我的SignInActivity.java:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_sign_in); 

     mAuth = FirebaseAuth.getInstance(); 

     mAuthListener = new FirebaseAuth.AuthStateListener() { 
      @Override 
      public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
       FirebaseUser user = firebaseAuth.getCurrentUser(); 
       if (user != null) { 
        // User is signed in 
        Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); 
       } else { 
        // User is signed out 
        Log.d(TAG, "onAuthStateChanged:signed_out"); 
       } 
       // ... 
      } 
     }; 

     mSignInToolbar = (Toolbar) findViewById(R.id.signInToolbar); 
     setSupportActionBar(mSignInToolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     mRef = FirebaseDatabase.getInstance().getReference().child("Users"); 
     mRef.keepSynced(true); 

     mEmailField = (EditText) findViewById(R.id.emailField); 
     mPasswordField = (EditText) findViewById(R.id.passowrdField); 
     mSigninBtn = (Button) findViewById(R.id.signinBtn); 
     mProgress = new ProgressDialog(this); 

     // Initialize Facebook Login button 
     FacebookSdk.sdkInitialize(getApplicationContext()); 
     mCallbackManager = CallbackManager.Factory.create(); 
     loginButton = (LoginButton) findViewById(R.id.button_facebook_login); 
     loginButton.setReadPermissions("email", "public_profile"); 
     loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       Log.d(TAG, "facebook:onSuccess:" + loginResult); 
       handleFacebookAccessToken(loginResult.getAccessToken()); 
      } 

      @Override 
      public void onCancel() { 
       Log.d(TAG, "facebook:onCancel"); 
       // ... 
      } 

      @Override 
      public void onError(FacebookException error) { 
       Log.d(TAG, "facebook:onError", error); 
       // ... 
      } 
     }); 

    } 


    @Override 
    public void onStart() { 
     super.onStart(); 
     mAuth.addAuthStateListener(mAuthListener); 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     // Pass the activity result back to the Facebook SDK 
     mCallbackManager.onActivityResult(requestCode, resultCode, data); 
    } 


    private void handleFacebookAccessToken(AccessToken token) { 
     Log.d(TAG, "handleFacebookAccessToken:" + token); 
     mProgress.setMessage("Logging in..."); 
     mProgress.show(); 

     AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken()); 
     mAuth.signInWithCredential(credential) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); 

         // 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(TAG, "signInWithCredential", task.getException()); 
          Toast.makeText(SignInActivity.this, "Authentication failed.", 
            Toast.LENGTH_SHORT).show(); 
          mProgress.dismiss(); 
         }else{ 
          String uid=task.getResult().getUser().getUid(); 
          String name=task.getResult().getUser().getDisplayName(); 
          String email=task.getResult().getUser().getEmail(); 
          String image=task.getResult().getUser().getPhotoUrl().toString(); 

          DatabaseReference childRef = mRef.child(uid); 
          childRef.child("name").setValue(name); 
          childRef.child("email").setValue(email); 
          childRef.child("image").setValue(image); 

          mProgress.dismiss(); 

          Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class); 
          mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(mainIntent); 


         } 

        } 
       }); 
    } 
+0

您是否發現問題所在? –

+0

是的,您只需要在Facebook開發者頁面中更改一些默認權限即可。請給我 –

+0

,我在這個問題上需要一些幫助。你能解釋一下這個解決方案嗎? –

回答

2

看看你需要做的是改變Facebook開發者頁面的一些權限。看到下面的圖片,請確保您也是如此做出必要的許可「是」: enter image description here

0
FacebookSdk.sdkInitialize(getApplicationContext()); 

在應用程序將這個。

+0

對不起,但它已經在那裏。 –

+0

創建一個擴展'Application'的新類'YourAppNameApplication'。重寫onCreate,把代碼放在那裏。 在您的manifest.xml中,在''下放置'android:name =「YourAppNameApplication'。 –

+0

我試過這種方法,但不工作 –

0

我已經通過在真實設備中運行應用程序(不在模擬器中)解決了我的問題。