Skip to content Skip to sidebar Skip to footer

Getting Email Address By Using Facebook Sdk 3.0.1 For Android

I'm currently developing an application which aims to authenticate any user by using Facebook acoount. I have a trouble in getting user email from user's account. My code is below

Solution 1:

Email is not a default field that's returned. Instead, you should create a meRequest, and pass it a parameter like: fields=email.

Request me = Request.newMeRequest(mCurrentSession, new GraphRequestCallback() {...});
Bundle params = me.getParameters();
params.putString("fields", "email,name");
me.setParameters(params);
me.executeAsync();

Solution 2:

you call just like this under regular things - only different you cannot directly user.email

System.out.println(user.asMap().get("email").toString());

regular :

        @Override
        publicvoidonClick(View v) {
            // start Facebook Login
            Session.openActiveSession(Giris.this, true, new Session.StatusCallback() {

              // callback when session changes state
              @Override
              publicvoidcall(Session session, SessionState state, Exception exception) {
                if (session.isOpened()) {

                  // make request to the /me API
                  Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                    // callback after Graph API response with user object
                    @Override
                    publicvoidonCompleted(GraphUser user, Response response) {
                      if (user != null) {
                          System.out.println(user.getName());
                          System.out.println(user.getBirthday());
                          System.out.println(user.getFirstName());
                          System.out.println(user.getLastName());
                          System.out.println(user.getLink());
                          System.out.println(user.getUsername());
                          System.out.println(user.getLocation());
                          System.out.println("facebook user id" + user.getId());
                         System.out.println(user.asMap().get("email").toString());
                         // Session.OpenRequest open = new Session.OpenRequest(Login)
                      }
                    }
                  });
                }
              }
            });
        }

Solution 3:

just call this method will return user email id to you.

privatevoiddoSocialNetworkinWithFacebook()
    {
        // check for session Session session=Session.getActiveSession();
         if (session != null && session.isOpened()) 
             {  
                // user is already login showtry
                        {
                            Session.OpenRequest request = newSession.OpenRequest(this);
                            request.setPermissions(Arrays.asList("email", "publish_actions"));
                        }
                    catch (Exception e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                 Request.executeMeRequestAsync(session, newRequest.GraphUserCallback() 
                        {
                          // callback after Graph API response with user object@OverridepublicvoidonCompleted(GraphUser user, Response response) 
                          {
                              if (user != null) 
                               {
                                   Toast.makeText(activity, "Welcome  "+user.getName(), Toast.LENGTH_SHORT).show();
                                  // publishFeedDialog(session);try
                                        {
                                            strFirstName = user.getFirstName().toString();
                                            strLocation = user.getLocation().getProperty("name").toString();
                                            strEmail = user.asMap().get("email").toString();

                                        }
                                    catch (Exception e)
                                        {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                            strEmail="";
                                        }

                                    runOnUiThread(newRunnable()
                                        {
                                            publicvoidrun()
                                                {
                                                    setUserInfoFromFacebook(strFirstName, strLocation, strEmail);
                                                }
                                        });
                                }
                          }
                    });

             }
         else
             {
                 // user is not log in //show  login screen// start Facebook Logintry
                        {
                            Session.OpenRequest request = newSession.OpenRequest(this);
                            request.setPermissions(Arrays.asList("email", "publish_actions"));
                        }
                    catch (Exception e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                 Session.openActiveSession(activity, true, newSession.StatusCallback() 
                 {

                     // callback when session changes state@Overridepublicvoidcall(final Session session, SessionState state, Exception exception) 
                    {
                        //session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("email")));Log.d(TAG, "Session :"+session.toString());
                        Log.d(TAG, "Session is opened :"+session.isOpened());

                        if (session.isOpened()) 
                        {                               
                            // make request to the /me APIRequest.executeMeRequestAsync(session, newRequest.GraphUserCallback()

                                {
                                  // callback after Graph API response with user object@OverridepublicvoidonCompleted(GraphUser user, Response response) 
                                  {
                                      if (user != null) 
                                       {

                                           Toast.makeText(activity, "Welcome  "+user.getName(), Toast.LENGTH_SHORT).show();
                                          // publishFeedDialog(session);try
                                            {
                                                    strFirstName = user.getFirstName().toString();
                                                    strLocation = user.getLocation().getProperty("name").toString();
                                                    strEmail = user.asMap().get("email").toString();
                                            }
                                        catch (Exception e)
                                            {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                                strEmail="";
                                            }

                                            runOnUiThread(newRunnable()
                                                {
                                                    publicvoidrun()
                                                        {
                                                            setUserInfoFromFacebook(strFirstName, strLocation, strEmail);
                                                        }
                                                });
                                        }
                                  }
                            });

                        }
                        elseif(session.isClosed())
                            {
                                 Toast.makeText(activity, "Unable to connect facebook, please try later..",Toast.LENGTH_SHORT).show();
                            }

                    }
                  });
             }

    }

Solution 4:

First add the appropriate permission, then fetch the email using the getProperty function.

permissions = Arrays.asList("email","languages","user_location","user_likes",    "user_education_history","user_work_history","user_hometown","user_about_me","user_status");

  Log.i(TAG, "user_email    : " + user.getProperty("email"));

Post a Comment for "Getting Email Address By Using Facebook Sdk 3.0.1 For Android"