Facebook登录请求通过身份验证后没有任何反应

时间:2015-01-06 19:57:09

标签: android facebook

我正在尝试通过Facebook登录Android应用。

点击Facebook登录按钮后,会打开一个新的网页视图,要求获得许可,我按OK然后关闭,应用程序已经关闭,因此没有。

public class Splash extends FragmentActivity {

    private MainFragment mainFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            // Add the fragment on initial activity setup
            mainFragment = new MainFragment();
            getSupportFragmentManager().beginTransaction() .add(android.R.id.content,mainFragment).commit();
        } else {
            // Or set the fragment from restored state info
            mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
        }
    }


public class MainFragment extends Fragment {


    private static final String TAG = "FBLoginFragment";
    private UiLifecycleHelper mUiHelper;
    private OnFBAccessTokenPass mTokenPasser;
    private String mSessionToken;


    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            Log.i(TAG, "Logged in...");
            mSessionToken = session.getAccessToken();
            // Request user data and show the results
            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        // pass the access token to the activity
                        // with the user id
                        mTokenPasser.onFBAccessTokenPass(mSessionToken, user.getId());
                    }
                }
            });
        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");
            mSessionToken = "";
        }
    }

    @Override
    public void onAttach(Activity a) {
        super.onAttach(a);
        //mTokenPasser = (OnFBAccessTokenPass) a;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUiHelper = new UiLifecycleHelper(getActivity(), callback);
        mUiHelper.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.login, container, false);

        LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
        authButton.setFragment(this);
        authButton.setReadPermissions(Arrays.asList("email"));

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        // For scenarios where the main activity is launched and user
        // session is not null, the session state change notification
        // may not be triggered. Trigger it if it's open/closed.
        Session session = Session.getActiveSession();
        if (session != null &&
                (session.isOpened() || session.isClosed())) {
            onSessionStateChange(session, session.getState(), null);
        }

        mUiHelper.onResume();
    }

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

    @Override
    public void onPause() {
        super.onPause();
        mUiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUiHelper.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mUiHelper.onSaveInstanceState(outState);
    }

    public interface OnFBAccessTokenPass {
        public void onFBAccessTokenPass(String accessToken, String uid);
    }
}

In Manifest

<activity
    android:name="com.facebook.LoginActivity"
    android:theme="@style/AppTheme" >
</activity>

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />

1 个答案:

答案 0 :(得分:1)

我认为问题在于主要活动得到了结果。因此,重写main的活动onActivityResult以允许片段获得结果将此添加到您的主要活动:

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

并更改authButton.setFragment(this)的位置;就在返回视图之前;