0

我试图开发一个用于从 facebook 进行用户身份验证的 android 应用程序我遵循了facebook 官方网站的程序

未进入 session.isopened() 的问题

 fb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                try {
                        PackageInfo info = getPackageManager().getPackageInfo(
                                "com.brightvision.dealsocl.mobileapp", 
                                PackageManager.GET_SIGNATURES);
                        for (Signature signature : info.signatures) {
                            MessageDigest md = MessageDigest.getInstance("SHA");
                            md.update(signature.toByteArray());
                            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
                            }
                    } catch (NameNotFoundException e) {

                    } catch (NoSuchAlgorithmException e) {

                    }

                 Session session = Session.getActiveSession();
                    if (!session.isOpened() && !session.isClosed()) {
                        Log.i("enter", "Session not opened");
                        /*Session.OpenRequest openRequest= new Session.OpenRequest(getParent());
                          openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
                            session.openForRead(openRequest);*/
         session.openForRead(new Session.OpenRequest(FrontPage.this).setCallback(statusCallback));
                    } else {


                        Session.openActiveSession(FrontPage.this,true,new StatusCallback() {

                            @Override
                            public void call(final Session session, SessionState state, Exception exception) {
                                // TODO Auto-generated method stub
                                if (session.isOpened()) {
                                    Log.i("enter", "Session opened");
                                    Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {

                                        @Override
                                        public void onCompleted(GraphUser user,Response response) {
                                            if (user != null) {

                                               String access_token = session.getAccessToken();
                                               String  firstName = user.getFirstName();
                                               String fb_user_id = user.getId();

                                               GraphObject graphObject = response.getGraphObject();
                                               JSONObject jsonObject = graphObject.getInnerJSONObject();

                                               try {
                                                String facebookEmail = jsonObject.getString("email");
                                                System.out.println("facebookEmail Login " + facebookEmail);
                                            } catch (JSONException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                            }

//                                           System.out.println("json object -> "+jsonObject);
//                                            
                                                System.out.println("First Name Login:"+ firstName);
                                                System.out.println("FB USER ID Login: " + fb_user_id);
                                           fname= firstName;

                                            }
                                        }
                                    });
                                    System.out.println("session is opened");
                                }else{
                                    System.out.println("session is not opened");

                                }
                                System.out.println("Session state "+state);
                                if(exception!=null){
                                    System.out.println("Exception in login "+exception.getMessage());
                                }
                            }
                        });
                    }



            }
        });




@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("on activity result", "enter");
        Session.getActiveSession().onActivityResult(FrontPage.this, requestCode, resultCode, data);
        Log.i("on activity result", "completed");

    }



    private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
           // your code. will execute when you are already logged in
             /* Intent i=new Intent(FrontPage.this,MapList.class);
              startActivity(i);*/

            Toast.makeText(getApplicationContext(), "welcome to facebook "+fname, 1000).show();

        }
    }

这是一个 Logcat:

06-27 16:36:31.412: D/KeyHash:(750): 844bro74Ov8hjX4YlTNgrx3IfT0= 06-27 16:36:31.412: I/enter(750): 会话未打开 06-27 16:36:31.592: I/Adreno200 -EGLSUB(750): : RGBA_8888 格式。

任何人都可以解决这个问题吗?

4

1 回答 1

0

试试这个。

在 onCreate() 之前声明这个

Session.StatusCallback statusCallback = new SessionStatusCallback();
Session session;

在您的 onCreate 中粘贴此代码

 session = Session.getActiveSession();
    if (session == null) {
        if (savedInstanceState != null) {
            session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
        }
        if (session == null) {
            session = new Session(this);
        }
        Session.setActiveSession(session);
        if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        }
    }

将以下代码添加到您的登录按钮

private void onClickLogin() {
        Session session = Session.getActiveSession();
        if (!session.isOpened() && !session.isClosed()) {
            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        } else {


            Session.openActiveSession(this,true,new StatusCallback() {

                @Override
                public void call(final Session session, SessionState state, Exception exception) {
                    // TODO Auto-generated method stub
                    if (session.isOpened()) {
                        Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {

                            @Override
                            public void onCompleted(GraphUser user,Response response) {
                                if (user != null) {

                                   String access_token = session.getAccessToken();
                                   String  firstName = user.getFirstName();
                                   String fb_user_id = user.getId();

                                   GraphObject graphObject = response.getGraphObject();
                                   JSONObject jsonObject = graphObject.getInnerJSONObject();

                                   try {
                                    String facebookEmail = jsonObject.getString("email");
                                    System.out.println("facebookEmail Login " + facebookEmail);
                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

//                                 System.out.println("json object -> "+jsonObject);
//                                  
                                    System.out.println("First Name Login:"+ firstName);
                                    System.out.println("FB USER ID Login: " + fb_user_id);

                                }
                            }
                        });
                        System.out.println("session is opened");
                    }else{
                        System.out.println("session is not opened");

                    }
                    System.out.println("Session state "+state);
                    if(exception!=null){
                        System.out.println("Exception in login "+exception.getMessage());
                    }
                }
            });
        }
    }

在您的课程中添加此方法

 private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
       // your code. will execute when you are already logged in
    }
}
于 2013-06-26T09:56:18.897 回答