2017-05-11 147 views
0

我想在我的應用程序中實現Facebook登錄,我已經在其他應用程序中使用過。所以在這個應用程序中,當我嘗試登錄時,登錄回調總是給出一個錯誤,我不知道是什麼導致它。如果我的代碼在任何地方出錯,請糾正我。Facebook的Android SDK登錄錯誤

public class LoginActivity extends AppCompatActivity { 

Button login; 
LoginButton loginButton; 
CallbackManager callbackManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FacebookSdk.sdkInitialize(getApplicationContext()); 
    AppEventsLogger.activateApp(this); 
    setContentView(R.layout.activity_login); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    login = (Button) findViewById(R.id.login); 
    login.setVisibility(View.GONE); 

    loginButton = new LoginButton(this); 
    loginButton.setReadPermissions("public_profile"); 

    AccessToken accessToken = AccessToken.getCurrentAccessToken(); 
    if (accessToken != null) { 
     startActivity(new Intent(LoginActivity.this, MainActivity.class)); 
     finish(); 
    } 

    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      login.setVisibility(View.VISIBLE); 
     } 
    }, 2000); 

    login.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      loginButton.performClick(); 
     } 
    }); 

    callbackManager = CallbackManager.Factory.create(); 
    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      // App code 
      startActivity(new Intent(LoginActivity.this, MainActivity.class)); 
      finish(); 
     } 

     @Override 
     public void onCancel() { 
      // App code 
      Log.d("LoginActivity", "Cancelled"); 
      Toast.makeText(getApplicationContext(), "Cancelled", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onError(FacebookException exception) { 
      // App code 
      Log.d("LoginActivity", "Error Occurred"); 
      Toast.makeText(getApplicationContext(), "Error Occurred", Toast.LENGTH_SHORT).show(); 
     } 

    }); 

} 

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

} 

而且我還添加了必要的東西在我的清單等的AppID和

<activity 
     android:name="com.facebook.CustomTabActivity" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 

      <data android:scheme="@string/fb_login_protocol_scheme" /> 
     </intent-filter> 
</activity> 
+0

看看日誌,看它是否有什麼東西 –

+0

它根本沒有說什麼。沒有一件事 –

+0

嘗試敬酒的FacebookException getMessage()或類似的東西 –

回答

1

這是因爲應用程序是在開發模式,使您的應用程序公開: