Facebook登录失败,安装了Facebook应用程序

时间:2013-04-10 15:32:31

标签: android facebook login

我是Android开发的新手。我已经下载了facebook sdk 3.0.1,并使用示例代码登录。问题是,如果安装了facebook app,则身份验证失败。 (否则它工作得很好) 按下登录按钮后,我进入onActivityResult,状态为CLOSED_LOGIN_FAILED。 当然我没有输入密码,它是通过Facebook的应用程序自动完成的,Facebook应用程序已登录并正常运行。 我找不到解决方案.. 请帮帮我:)。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    instance = this;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    buttonLoginToFacebook = (Button) findViewById(R.id.buttonLoginToFacebook);
    tvWellcome = (TextView) findViewById(R.id.tvWellcome);

    Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

    Session session = Session.getActiveSession();
    if (session == null) {
        if (savedInstanceState != null) {
            session = Session.restoreSession(this, null, statusCallback,
                    savedInstanceState);
        }
        if (session == null) {
            session = new Session(this);
        }
        Session.setActiveSession(session);
        if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
            session.openForRead(new Session.OpenRequest(this)
                    .setCallback(statusCallback));
        }
    }

    updateView();
}

@Override
public void onStart() {
    super.onStart();
    Session.getActiveSession().addCallback(statusCallback);
}

@Override
public void onStop() {
    super.onStop();
    Session.getActiveSession().removeCallback(statusCallback);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode,
            resultCode, data);
    updateView();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Session session = Session.getActiveSession();
    Session.saveSession(session, outState);
}

private void updateView() {
    Session session = Session.getActiveSession();
    if (session.isOpened()) {
        Intent intent = new Intent(this, ScreenStartPage.class);
        startActivity(intent);
    } else {
        buttonLoginToFacebook.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                onClickLogin();
            }
        });
    }
}

private void onClickLogin() {
    Session session = Session.getActiveSession();
    if (!session.isOpened() && !session.isClosed()) {
        session.openForRead(new Session.OpenRequest(this)
                .setCallback(statusCallback));
        updateView();
    } else {
        Session.openActiveSession(this, true, statusCallback);
    }
}


public void onClickLogout() {
    Session session = Session.getActiveSession();
    if (!session.isClosed()) {
        session.closeAndClearTokenInformation();
    }
}

private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state,
            Exception exception) {
        updateView();
    }
}

1 个答案:

答案 0 :(得分:0)

您是否在Facebook应用设置信息中心设置了调试密钥哈希和完整的班级名称?请参阅Getting Started with the Facebook SDK for Android doc。

中的步骤5和6