Facebook登录Android取消

时间:2015-04-22 21:47:33

标签: android facebook facebook-graph-api facebook-login facebook-sdk-4.0

我想在Android应用程序中使用Facebook登录,在日志中我收到“取消”。这是代码,任何想法?

我遵循developers.facebook.com的指示,我尝试使用片段登录。

public class LoginFragment extends Fragment {

public static final String TAG = LoginFragment.class.getSimpleName();

private CallbackManager callbackManager;

private ProfileTracker profileTracker;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

    setUpCallbacks();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_login, container, false);

    LoginButton loginButton = (LoginButton) view.findViewById(R.id.facebook_button_login);

    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final LoginManager manager = LoginManager.getInstance();
            manager.setLoginBehavior(LoginBehavior.SSO_WITH_FALLBACK);
            manager.logInWithReadPermissions(LoginFragment.this, null);
        }
    });

    return view;
}

private void setUpCallbacks() {
    callbackManager = CallbackManager.Factory.create();
    LoginManager manager = LoginManager.getInstance();
    manager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Profile.fetchProfileForCurrentAccessToken();
            Log.d(TAG, "success");
        }

        @Override
        public void onError(FacebookException exception) {
            AccessToken.setCurrentAccessToken(null);
            Log.d(TAG, "error");
        }

        @Override
        public void onCancel() {
            AccessToken.setCurrentAccessToken(null);
            Log.d(TAG, "cancel");
        }

    });

    profileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
            AccessToken currentAccessToken = AccessToken.getCurrentAccessToken();
            if (currentAccessToken == null) {
                Log.d(TAG, "AccessToken is null");
            }
            else {
                Log.d(TAG, currentProfile.getName());
            }
        }
    };
}

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

    callbackManager.onActivityResult(requestCode, resultCode, data);
}

}

感谢。

1 个答案:

答案 0 :(得分:0)

LoginButton使用如下:

    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions(Arrays.asList("user_friends, public_profile, email, user_birthday"));

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {

        }
    });