3

在尝试访问用户详细信息时,我收到此错误:

07-29 14:49:47.343: W/System.err(2745): org.json.JSONException: No value for email
07-29 14:49:47.353: W/System.err(2745):     at org.json.JSONObject.get(JSONObject.java:354)
07-29 14:49:47.374: W/System.err(2745):     at org.json.JSONObject.getString(JSONObject.java:510)
07-29 14:49:47.374: W/System.err(2745):     at com.example.healthcity.common.FacebookContainer$3$1.onCompleted(FacebookContainer.java:237)
07-29 14:49:47.409: W/System.err(2745):     at com.facebook.Request$1.onCompleted(Request.java:264)
07-29 14:49:47.413: W/System.err(2745):     at com.facebook.Request$4.run(Request.java:1240)
07-29 14:49:47.423: W/System.err(2745):     at android.os.Handler.handleCallback(Handler.java:725)
07-29 14:49:47.423: W/System.err(2745):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-29 14:49:47.433: W/System.err(2745):     at android.os.Looper.loop(Looper.java:137)
07-29 14:49:47.444: W/System.err(2745):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-29 14:49:47.444: W/System.err(2745):     at java.lang.reflect.Method.invokeNative(Native Method)
07-29 14:49:47.463: W/System.err(2745):     at java.lang.reflect.Method.invoke(Method.java:511)
07-29 14:49:47.463: W/System.err(2745):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-29 14:49:47.485: W/System.err(2745):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-29 14:49:47.493: W/System.err(2745):     at dalvik.system.NativeStart.main(Native Method)

我无法在回调方法上捕获断点。如果您遇到此错误,请帮助。

以下是我获取用户详细信息的代码:

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    //RFR//_("onSessionStateChange----------------");
   /////////////// super.onSessionStateChange(state, exception);
    if (state.isOpened()) {
        readUserInfo(session);
    } else if (state.isClosed()) {
        // Session closed
    }
}

private Request.GraphUserCallback graphCallback = new Request.GraphUserCallback() {

      // callback after Graph API response with user object
      @Override
      public void onCompleted(GraphUser user, Response response) {
          String name = user.getName();
          String gender = (String) user.getProperty("gender");
          String email = (String) user.getProperty("email");
          String birthday = user.getBirthday();
          Log.d("FB----->","name: "+ name +" gender: "+ gender +" email: "+ email +" birthday: " +birthday);

      }

};

    public void readUserInfo(Session session){
        // make request to the /me API
        Request.executeMeRequestAsync(session, graphCallback);
    }

以下是我设置的权限:

openRequest.setPermissions(Arrays.asList("user_birthday", "email", "user_location"));

这是我从浏览器检查时从图形 api 得到的响应:

{
  "id": "1000000XXXXX930",
  "name": "First Last",
  "first_name": "First",
  "last_name": "Last",
  "link": "http://www.facebook.com/<myusername>",
  "username": "<myusername>",
  "gender": "male",
  "locale": "en_US"
}
4

6 回答 6

4
  • 抛出异常,因为您正在尝试访问电子邮件
  • 但是用户没有使用电子邮件进行注册,而是使用了电话号码进行注册。
  • 您必须在代码中检查子响应是否具有密钥的可能性,然后才尝试提取该密钥的值

if(object.has("email")){
   userEmail = object.getString("email");
}
于 2018-12-04T08:02:57.323 回答
1

您是否授予“电子邮件”权限以请求 Facebook 应用程序访问任何用户的电子邮件 ID?

如果您想使用 API 访问 Facebook 用户的电子邮件 ID,那么您必须授予电子邮件权限。有关用户个人资料的一般信息不会为您提供 Facebook 用户的电子邮件地址。

Facebook API 中的电子邮件权限

注意:一些用户还设置了安全性,不与任何其他人共享任何电子邮件地址,那么您也可能无法获得电子邮件地址。

希望它会帮助你。

于 2013-07-29T09:29:26.103 回答
0

这段代码在获得所需权限方面与我配合良好,这也将帮助您获得电子邮件权限

  private void FaceBookSignIn()
   {
   facebookBtn.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View arg0) {
       Session currentSession = Session.getActiveSession();
        if (currentSession == null || currentSession.getState().isClosed()) {
            Session session = new Session.Builder(context).build();
            Session.setActiveSession(session);
            currentSession = session;
        }

        if (currentSession.isOpened()) {
               Session.openActiveSession(SignIn_Rewards.this, false, new Session.StatusCallback() {

                      @Override
                      public void call(final Session session, SessionState state,
                              Exception exception) {

                          if (session.isOpened()) {

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

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

                                                  fromFB=true;

                                                 UserID=user.getId();
                                                 firstName=user.getFirstName();
                                                 Lastname=user.getLastName();
                                                 Gender=user.getProperty("gender").toString();
                                                 Email=user.getProperty("email").toString();
                                                 DateofBirth=user.getBirthday();
                                                try {
                                                    getToken();
                                                } catch (NoSuchAlgorithmException e) {
                                                    // TODO Auto-generated catch block
                                                    e.printStackTrace();
                                                }

                                              }
                                          }
                                      });
                          }
                      }
                  });
              }
            // Do whatever u want. User has logged in

         else if (!currentSession.isOpened()) {
            // Ask for username and password
            OpenRequest op = new Session.OpenRequest((Activity) context);

            op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
            op.setCallback(null);

            List<String> permissions = new ArrayList<String>();
            permissions.add("publish_stream");
            permissions.add("user_likes");
            permissions.add("email");
            permissions.add("user_birthday");
            op.setPermissions(permissions);

            Session session = new Session.Builder(SignIn_Rewards.this).build();
            Session.setActiveSession(session);
            session.openForPublish(op);
        }




   }
  }) ;

      }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (Session.getActiveSession() != null)
          Session.getActiveSession().onActivityResult(this, requestCode,
                  resultCode, data);

      Session currentSession = Session.getActiveSession();
      if (currentSession == null || currentSession.getState().isClosed()) {
          Session session = new Session.Builder(context).build();
          Session.setActiveSession(session);
          currentSession = session;
      }

      if (currentSession.isOpened()) {
          Session.openActiveSession(this, true, new Session.StatusCallback() {

              @Override
              public void call(final Session session, SessionState state,
                      Exception exception) {

                  if (session.isOpened()) {

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

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

                                          fromFB=true;

                                         UserID=user.getId();
                                         firstName=user.getFirstName();
                                         Lastname=user.getLastName();
                                         Gender=user.getProperty("gender").toString();
                                         Email=user.getProperty("email").toString();
                                         DateofBirth=user.getBirthday();
                                        try {
                                            getToken();
                                        } catch (NoSuchAlgorithmException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }

                                      }
                                  }
                              });
                  }
              }
          });
      }
  }
于 2013-07-29T09:43:54.247 回答
0

你有这样的检查:

private Request.GraphUserCallback graphCallback = new Request.GraphUserCallback() {

  // callback after Graph API response with user object
  @Override
  public void onCompleted(GraphUser user, Response response) {
      String name = user.getName();
      String gender = (String) user.getProperty("gender");
      String email = (String) user.getProperty("email");
      String birthday = user.getBirthday();

     if(email!=null){
     //perform your task here.
     }else{
     Log.v("email","is empty");
     }

      Log.d("FB----->","name: "+ name +" gender: "+ gender +" email: "+ email +" birthday: " +birthday);

  }};
于 2013-07-29T11:38:33.977 回答
0

试试这个以获得电子邮件的价值 已 编辑:默认情况下,您不会收到电子邮件,对于电子邮件,您必须指定权限。

Session.StatusCallback statusCallback = new Session.StatusCallback() 
        {

            @Override
            public void call(Session session, SessionState state, Exception exception) 
            {
                if(session.isOpened())
                {
                    Request.executeMeRequestAsync(session, new Request.GraphUserCallback() 
                    {

                        @Override
                        public void onCompleted(GraphUser user, Response response) 
                        {

                            if(user != null)
                            {
                                try
                                {
                                    System.out.println("Graph Inner Json"+user.getInnerJSONObject());
                                    String email = user.getInnerJSONObject().getString("email");
                                }
                             }
                         }
                     }
                 }
            }

YourActivityName.openActiveSession(this, true, statusCallback);

这是openActiveSession()我们指定权限的地方

private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback statusCallback)
    {
        OpenRequest openRequest = new OpenRequest(activity);
        openRequest.setPermissions(Arrays.asList("user_birthday", "email"));
        openRequest.setCallback(statusCallback);

        Session session = new Session.Builder(activity).build();

        if(SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI)
        {
            Session.setActiveSession(session);
            session.openForRead(openRequest);

            return session;
        }

        return null;
    }

最后,您应该在onActivityResultMethod 中执行以下操作:

if(Session.getActiveSession() != null)
        {
            Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
        }
于 2013-11-05T11:39:13.763 回答
0

您确实需要获取某人的电子邮件的权限,尽管有些人设置了他们的隐私,这样您就可以在没有此明确许可的情况下获取他们的电子邮件,大多数人不需要。

于 2013-07-29T09:29:54.837 回答