Facebook登录无法正常使用其他人

时间:2014-01-04 07:47:34

标签: android android-facebook

我正在我的应用程序中使用facebook sdk登录facebook,其中授权人(我在facebook上创建了示例)成功登录但是当另一个人登录时他无法登录(我认为会话已创建但会话状态已关闭)现在我很困惑我的工作。 我的代码在下面

 public class MainFragment extends Fragment{

    private static final String TAG = "deepak";
    private UiLifecycleHelper uiHelper;
    Boolean isInternetPresent;
    private TextView albumTextView;
    private static String APP_ID = "xxxxxxxxxxxxxxxxx";
    Facebook facebook;
    Session session;
    private boolean flag = false;
    static ArrayList<String> allNames;
    static  ArrayList<String> uid;
    static ArrayList<String> picUrl;

//    String[] pics =  new String[]{};
//    String[] uids;
//    String[] allnames;

    JSONArray jArray;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
        // TODO Auto-generated method stub
         View view = inflater.inflate(R.layout.activity_main, container, false);
         LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
         authButton.setReadPermissions(Arrays.asList("read_friendlists", "user_photos", "friends_photos"));
         albumTextView = (TextView) view.findViewById(R.id.text_viet_albul);
         authButton.setFragment(this);
         facebook = new Facebook(APP_ID);
         allNames = new ArrayList<String>();
         uid = new ArrayList<String>();
         picUrl = new ArrayList<String>();

         ConnectionDetector cd = new ConnectionDetector(getActivity()); 
         isInternetPresent = cd.isConnectingToInternet();
         checkInternetConnection();

         return view;
    }

    private void checkInternetConnection() {
        // TODO Auto-generated method stub
        if(!isInternetPresent) {                    
            // Internet connection is not present                     
            // Ask user to connect to Internet                    
            showAlertDialog(getActivity(), "No Internet Connection", "You don't have internet connection.", false);                
            } 
        }


     public void showAlertDialog(Context context, String title, String message, Boolean status) {
         AlertDialog alertDialog = new AlertDialog.Builder(context).create();          
         // Setting Dialog Title         
         alertDialog.setTitle(title);           
         // Setting Dialog Message         
         alertDialog.setMessage(message);                   
         // Setting alert dialog icon         
         //alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);           
         // Setting OK Button         
         alertDialog.setButton("OK", new DialogInterface.OnClickListener() 
         {            
             public void onClick(DialogInterface dialog, int which)
             {    
                 MainActivity.finishIfNetworkIsNotWorking();
             }        
         });           
         // Showing Alert Message         
         alertDialog.show();     

     }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            Log.i(TAG, "Logged in...");
        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");

        }
    }

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

        }
    };

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

        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);

    }


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

        Session session = Session.getActiveSession();
        if (session != null &&
               (session.isOpened() || session.isClosed()) ) {
            onSessionStateChange(session, session.getState(), null);
        }
       uiHelper.onResume();       
    }

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


    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();

    }

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

    }
} 

2 个答案:

答案 0 :(得分:0)

更改用户时是否注销?有时您的令牌或会话会保存在共享首选项中,当您运行应用程序时,它会从文件中加载令牌并创建会话。 您的方法注销必须如下:

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

你有活跃的会话吗?

答案 1 :(得分:0)

使用

Session.getActiveSession().closeAndClearTokenInformation();

获取足够的详细信息或使用登录帐户完成流程后。因此,当再次登录时,它会自动从Facebook用户中查找新的令牌。