1

我目前正在开发一个应用程序,旨在通过使用 Facebook 帐户对任何用户进行身份验证。

我在从用户帐户获取用户电子邮件时遇到问题。我的代码如下

    private void signInWithFacebook() {

    SessionTracker mSessionTracker = new SessionTracker(getBaseContext(), new StatusCallback() {

        public void call(Session session, SessionState state, Exception exception) {
        
        }
    }, null, false);

    String applicationId = Utility.getMetadataApplicationId(getBaseContext());
    Session mCurrentSession = mSessionTracker.getSession();

    if (mCurrentSession == null || mCurrentSession.getState().isClosed()) {
        mSessionTracker.setSession(null);
        Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
        Session.setActiveSession(session);
        mCurrentSession = session;
    }

    if (!mCurrentSession.isOpened()) {
        Session.OpenRequest openRequest = null;
        openRequest = new Session.OpenRequest(FacebookLoginActivity.this);

        if (openRequest != null) {
            openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
            openRequest.setPermissions(Arrays.asList("user_birthday", "email", "user_location"));
            openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);

            mCurrentSession.openForRead(openRequest);
        }
    }else {
        Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
              public void onCompleted(GraphUser user, Response response) {
                  Log.w("myConsultant", user.getId() + " " + user.getName() + " " + user.getLink() + "  "+ response);
              }
            });
    }
}

我正在使用适用于 Android 的 Facebook SDK 3.0.1。我已设置 Facebook Graph Api 所需的权限。在响应xml中,没有像电子邮件这样的字段。Facebook Sdk 文档不够好,我不知道如何获取电子邮件地址。

提前致谢。

4

4 回答 4

14

电子邮件不是返回的默认字段。相反,您应该创建一个 meRequest,并向其传递一个参数,例如:fields=email。

Request me = Request.newMeRequest(mCurrentSession, new GraphRequestCallback() {...});
Bundle params = me.getParameters();
params.putString("fields", "email,name");
me.setParameters(params);
me.executeAsync();
于 2013-04-17T01:24:42.967 回答
4

您在常规情况下就像这样打电话 - 唯一不同的是您不能直接 user.email

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

常规的 :

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

              // callback when session changes state
              @Override
              public void call(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
                    public void onCompleted(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)
                      }
                    }
                  });
                }
              }
            });
        }
于 2013-07-26T02:52:14.690 回答
1

只需调用此方法即可将用户电子邮件 ID 返回给您。

private void doSocialNetworkinWithFacebook()
    {
        // check for session 
         Session session=Session.getActiveSession();
         if (session != null && session.isOpened()) 
             {  
                // user is already login show
                    try
                        {
                            Session.OpenRequest request = new Session.OpenRequest(this);
                            request.setPermissions(Arrays.asList("email", "publish_actions"));
                        }
                    catch (Exception e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                 Request.executeMeRequestAsync(session, new Request.GraphUserCallback() 
                        {
                          // callback after Graph API response with user object

                          @Override
                          public void onCompleted(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(new Runnable()
                                        {
                                            public void run()
                                                {
                                                    setUserInfoFromFacebook(strFirstName, strLocation, strEmail);
                                                }
                                        });
                                }
                          }
                    });

             }
         else
             {
                 // user is not log in 
                 //show  login screen

                // start Facebook Login

                    try
                        {
                            Session.OpenRequest request = new Session.OpenRequest(this);
                            request.setPermissions(Arrays.asList("email", "publish_actions"));
                        }
                    catch (Exception e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                 Session.openActiveSession(activity, true, new Session.StatusCallback() 
                 {

                     // callback when session changes state
                    @Override
                    public void call(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 API

                            Request.executeMeRequestAsync(session, new Request.GraphUserCallback()

                                {
                                  // callback after Graph API response with user object
                                  @Override
                                  public void onCompleted(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(new Runnable()
                                                {
                                                    public void run()
                                                        {
                                                            setUserInfoFromFacebook(strFirstName, strLocation, strEmail);
                                                        }
                                                });
                                        }
                                  }
                            });

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

                    }
                  });
             }

    }
于 2013-06-28T10:00:48.843 回答
0

首先添加适当的权限,然后使用该getProperty功能获取电子邮件。

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"));
于 2015-03-14T11:26:44.807 回答