且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重新开放申请后,最后再利用Facebook的登录会话

更新时间:2023-11-28 11:41:46

我通过调用allowLoginUI =虚假的onCreate()我活动的方法openActiveSession方法解决它。
我的code是 -

  ParseFacebookUtils.getSession()。openActiveSession(这一点,假的,新Session.StatusCallback(){@覆盖
公共无效呼叫(会话的会话,SessionState会状态,异常除外)
{
     如果(session.isOpened()){        ParseFacebookUtils.logIn(LoginActivity.this,
                        新LogInCallback(){                            @覆盖
                            公共无效完成(ParseUser用户,ParseException的E){
                            //用户登录
                            }                    });
              }
              其他
              {
                //发现错误
              }
           }
        });

I want to re-login a Facebook user in parse without showing login dialog of Facebook using saved TOKEN in Parse.

I am using 3 methods for save and restore session but how to use them in Facebook object of ParseFacebookUtils?

/*
 * Save the access token and expiry date so you don't have to fetch it each
 * time
 */
public static boolean save(Facebook session, Context context) {
    Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
    editor.putString(TOKEN, session.getAccessToken());
    editor.putLong(EXPIRES, session.getAccessExpires());
    editor.putLong(LAST_UPDATE, session.getLastAccessUpdate());
    return editor.commit();
}

/*
 * Restore the access token and the expiry date from the shared preferences.
 */
public static boolean restore(Facebook session, Context context) {
    SharedPreferences savedSession = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
    session.setTokenFromCache(
            savedSession.getString(TOKEN, null),
            savedSession.getLong(EXPIRES, 0),
            savedSession.getLong(LAST_UPDATE, 0));
    return session.isSessionValid();
}

public static void clear(Context context) {
    Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
    editor.clear();
    editor.commit();
}

And i am restoring session by following before login-

    ParseFacebookUtils.initialize(APP_ID);
    // restore session
    restore(ParseFacebookUtils.getFacebook(), this);
    // login using restored data
    ParseFacebookUtils.logIn(permissions, LoginActivity.this,
            new LogInCallback() {
                @SuppressWarnings("deprecation")
                @Override
                public void done(ParseUser user, ParseException e) {

                // work after login

                        ParseUser.enableAutomaticUser();
                        save(ParseFacebookUtils.getFacebook(),this);
                }});

But it's not working it always opens login dialog of facebook.

I solved it by calling openActiveSession method with allowLoginUI = false in onCreate() method of my Activity. My code is-

ParseFacebookUtils.getSession().openActiveSession(this, false, new Session.StatusCallback() {

@Override
public void call(Session session, SessionState state, Exception exception)
{
     if (session.isOpened()) {

        ParseFacebookUtils.logIn( LoginActivity.this,
                        new LogInCallback() {

                            @Override
                            public void done(ParseUser user, ParseException e) {
                            // user logged in
                            }

                    });
              }
              else 
              {
                // error found
              }
           }
        });