1

I want to have facebook login using graph Api because I want user_birthday and user_location. Before I was using this link to login: http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/

But I was not getting additional information like birth date and location. So I am approaching towards graph Api. But didn't find any tutorial for it. I tried some link and build some code but getting session null. I am sure thats a implementation issue. Here is my code:

     fbLogin.setOnClickListener( new OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                openActiveSession(LoginScreenActivity.this, true, fbStatusCallback, Arrays.asList(
                        new String[] { "email",  "user_location", "user_birthday"}), savedInstanceState);
            }
            catch (Exception e) {
                Log.i("exception", e.getMessage());
            }                   
        Intent intent=new Intent(LoginScreenActivity.this,DealGaliTabActivity.class);
        startActivity(intent);

        }
    });

  private  Session openActiveSession(Activity activity, boolean allowLoginUI,
              StatusCallback callback, List<String> permissions, Bundle savedInstanceState) {
            OpenRequest openRequest = new OpenRequest(activity)
            .setPermissions(permissions).setLoginBehavior(SessionLoginBehavior
            .SSO_WITH_FALLBACK).setCallback(callback)
            .setDefaultAudience(SessionDefaultAudience.FRIENDS);

                 Session session = Session.getActiveSession();
                 Log.d("session", "" + session);
                 if (session == null) {
                     Log.d("sussion_Null", "" + savedInstanceState);
                     if (savedInstanceState != null) {
                         session = Session.restoreSession(this, null, fbStatusCallback, savedInstanceState);
                     }
                     session = Session.openActiveSessionFromCache(LoginScreenActivity.this);
                     Session.setActiveSession(session);
                     Log.d("sessionSet", "" + session);

                     if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED) || allowLoginUI) {
                         session.openForRead(openRequest);
                         return session;
                     }
                 }
                 return session;
             }
             @Override
             public void onActivityResult(int requestCode, int resultCode, Intent data) {
                 super.onActivityResult(requestCode, resultCode, data);
                 Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
             }

             private Session.StatusCallback fbStatusCallback = new Session.StatusCallback() {
                    public void call(Session session, SessionState state, Exception exception) {
                        if (state.isOpened()) {
                            Request.newMeRequest(session, new Request.GraphUserCallback() {
                                public void onCompleted(GraphUser user, Response response) {
                                    if (response != null) {
                                     Log.i("Graphresponse", response.toString());
                                        try{
                                            get_id = user.getId();
                                            get_name = user.getName();
                                            get_gender = (String) user.getProperty("gender");
                                            get_email = (String) user.getProperty("email");
                                            get_birthday = user.getBirthday();
                                            get_locale = (String) user.getProperty("locale");
                                            get_location = user.getLocation().toString();   

                                        Log.d("GraphSelected", user.getId() + "; " +  
                                            user.getName() + "; " +
                                            (String) user.getProperty("gender") + "; " +        
                                            (String) user.getProperty("email") + "; " +
                                            user.getBirthday()+ "; " +
                                            (String) user.getProperty("locale") + "; " +
                                            user.getLocation());
                                        } catch(Exception e) {
                                             e.printStackTrace();
                                             Log.d("GraphException", e.getMessage());
                                         }

                                    }
                                }
                            });
                        }
                    }
                }; 

I am also not getting facebook screen for first time login..please suggest me some solution to it.

2 Answers 2

1

I'm not really sure if getBirthday(), getLocation() are the valid methods, but you can directly access these properties as-

get_birthday = (String) user.getProperty("birthday");
get_location = (String) user.getProperty("location").getProperty("name");

I'm also not really sure user.getProperty("location").getProperty("name") is valid thing to do, actually the response of location is of form:

"location": {
     "id": 123456789,
     "name": "Blah"
}

This should get you the birthday and location.


Also, check if you can see the bday and location permission granted or not in the app settings

1

This is what I want..I hope it would be helpful to others...

public void readData(){
     if (Session.getActiveSession() == null
                || Session.getActiveSession().isClosed()) {
            Session.openActiveSession(this, true, new StatusCallback() {

                @Override
                public void call(Session session, SessionState state,
                        Exception exception) {
                    System.out.println("State= " + state);

                    if (session.isOpened()) {

                        System.out.println("Token=" + session.getAccessToken());
                        if(session.getAccessToken() != null){
                            sharedPrefernces();
                        }
                            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                            public void onCompleted(GraphUser user, Response response) {
                                if (response != null) {
                                 Log.i("Graphresponse", response.toString());
                                    try{
                                        get_id = user.getId();
                                        get_name = user.getName();
                                        get_gender = (String) user.getProperty("gender");
                                        get_email = (String) user.getProperty("email");
                                        get_birthday = user.getBirthday();
                                        get_locale = (String) user.getProperty("locale");
                                        get_location = user.getLocation().toString(); 

                                        URL img_value = null;
                                        img_value = new URL("http://graph.facebook.com/"+get_id+"/picture?type=large");
                                        mImage = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());

                                    Log.d("GraphSelected", get_id + "  " +  
                                        get_name + "  " +
                                        get_gender + "  " +        
                                        get_email + "  " +
                                        get_birthday + "  " +
                                        get_locale + "  " +
                                        get_location);
                                    } catch(Exception e) {
                                         e.printStackTrace();
                                         Log.d("GraphException", e.getMessage());
                                     }
                                }
                            }
                        });
                    }
                    if (exception != null) {
                        System.out.println("Some thing bad happened!");
                        exception.printStackTrace();
                    }
                }
            });
        }
     }  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.