Using facebook login correctly

时间秒杀一切 提交于 2019-12-03 16:39:41

Do not start a new session .... change it in the source code

private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
        Exception exception) {
    onSessionStateChange(session, state, exception);
}
};
Usman Afzal
  • Add the facebook SDK in your project
  • Add below lines in AndroidManifest.xml under tag

        <activity android:name="com.facebook.LoginActivity" />
    
  • Also Add facebook app id in string.xml

  • Declare the private members

    private boolean isOnCreate;
    private List<String> permissions = new ArrayList<String>();
    private JSONObject facebookObject;
    private ProgressDialog progressDialog;
    
    /** The callback. */
    private final com.facebook.Session.StatusCallback callback = new 
    
    com.facebook.Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
    SignInActivity.this.onSessionStateChange(session, state, exception);
    }
    };
    
    private void onSessionStateChange(Session session, SessionState state, Exception         exception) {
    
    if (state == SessionState.OPENED) {
    if (this.isOnCreate) {
     this.isOnCreate = false;
     return;
         }
         onFacebookLoginDone();
    } else if (state == SessionState.CLOSED_LOGIN_FAILED) {
    showErrorMessage();
    }
    }
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Session session = Session.getActiveSession();
        if (session == null) {
            session = new Session(this);
            session.closeAndClearTokenInformation();
        }
        Session.setActiveSession(session);
        }
    
        @Override
        public void onClick(View v) {
        switch (v.getId()) {
    
        case R.id.btn_fb_login:
            this.loginUsingFacebook();
            break;
    
        default:
            break;
        }
    
        }
    
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
        }
    
        /**
         * Login using facebook.
         */
        private void loginUsingFacebook() {
        isOnCreate = false;
        final Session session = Session.getActiveSession();
        if (session != null && session.getState().equals(SessionState.OPENED)) {
            Session.openActiveSession(this, true, this.callback);
            onFacebookLoginDone();
        } else {
            final Session.OpenRequest temp = new Session.OpenRequest(this);
            this.permissions.clear();
            this.permissions.add(UserClass.EMAIL);
            this.permissions.add("user_location");
            temp.setPermissions(this.permissions);
            temp.setCallback(this.callback);
            session.openForRead(temp);
        }
    
        }
    
        /**
         * On facebook login done.
         */
        private void onFacebookLoginDone() {
    
        final Bundle bundle = new Bundle();
        bundle.putString("fields", "first_name,last_name,id,location,locale,username,email,verified");
        new PerformSignUpOnServer("/me", bundle).execute();
        }
    
        /**
         * The Class PerformSignUpOnServer.
         */
        private class PerformSignUpOnServer extends AsyncTask<Void, Void, Void> {
    
        /** The server url. */
        private String serverURL;
    
        /** The server bundle. */
        private Bundle serverBundle;
    
        /**
         * Instantiates a new perform sign up on server.
         * 
         * @param url
         *            the url
         * @param bundle
         *            the bundle
         */
        public PerformSignUpOnServer(String url, Bundle bundle) {
    
            this.serverBundle = bundle;
            this.serverURL = url;
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(SignInActivity.this, "", "Loading...");
            super.onPreExecute();
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Void doInBackground(Void... params) {
    
            signUpUserOnServer(this.serverURL, this.serverBundle);
    
            return null;
    
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Void result) {
    
    
        }
        }
    
        /**
         * Sign up user on server.
         * 
         * @param url
         *            the url
         * @param bundle
         *            the bundle
         */
        private void signUpUserOnServer(String url, Bundle bundle) {
        final Session session = Session.getActiveSession();
        if (session != null && session.isClosed() == false) {
            final Request userInformation = new Request(session, url, bundle, HttpMethod.GET);
            final Response response = userInformation.executeAndWait();
            facebookObject = response.getGraphObject().getInnerJSONObject();
            Log.d("json", facebookObject.toString());
        }
        }
    
        /**
         * On session state change.
         * 
         * @param state
         *            the state
         */
        private void onSessionStateChange(SessionState state) {
    
        if (state == SessionState.OPENED) {
            if (isOnCreate) {
            isOnCreate = false;
            return;
            }
            onFacebookLoginDone();
        }
        }
    

    See your log cat with tag "json" you will have a facebook object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!