会话Facebook不起作用

时间:2013-06-12 09:22:28

标签: android facebook

我尝试使用我的Android应用程序连接到Facebook的第一个教程,它只有在我的手机上没有官方Android应用程序时才有效,但是当我这样做时,会话没有打开

这是我的代码:

public class LoginActivity extends Activity {
TextView tv_name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    tv_name = (TextView) findViewById(R.id.tv_activity_login);
    startFbLogin();
}

private void startFbLogin() {
    // start Facebook LoginActivity
    Session.openActiveSession(this, true, new Session.StatusCallback() {

        // callback when session changes state
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {
                Log.i("FaceBook", "session is Opened");
                // make request to the /me API
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            Log.i("FaceBook", "user is not null");
                            tv_name.setText("Hello " + user.getName() + "!");
                        }else{
                            Log.i("FaceBook", "user is null");
                            logout();
                        }
                    }
                });
            }else{
                Log.i("FaceBook", "is not Opened");
            }
        }
    });

}
 @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
  }
 private void logout(){
        // clear any user information
        //mApp.clearUserPrefs();
        // find the active session which can only be facebook in my app
        Session session = Session.getActiveSession();
        // run the closeAndClearTokenInformation which does the following
        // DOCS : Closes the local in-memory Session object and clears any persistent 
        // cache related to the Session.
        session.closeAndClearTokenInformation();
        // return the user to the login screen
        startActivity(new Intent(getApplicationContext(), LoginActivity.class));
        // make sure the user can not access the page after he/she is logged out
        // clear the activity stack
        finish();
    }
}

2 个答案:

答案 0 :(得分:0)

您是否检查过您是否拥有该应用的互联网权限?

另外。您似乎没有遵循建议的登录方式。请访问Facebook Login Flow for Android并尝试从那里推荐的方法(例如使用UiLifecycleHelper)。

祝你好运!

答案 1 :(得分:0)

好的问题是我在应用程序中放入了错误的哈希键。