0

我尝试了第一个教程以使用我的 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();
    }
}
4

2 回答 2

0

您是否检查过您是否拥有该应用程序的 Internet 权限?

还。您似乎没有遵循推荐的登录方式。请访问Android 版 Facebook 登录流程并从那里尝试推荐的方法(例如使用 UiLifecycleHelper)。

祝你好运!

于 2013-06-12T09:49:55.377 回答
0

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

于 2013-06-12T16:03:21.637 回答