与facebook connect一起使用时,JSON不会收到电子邮件

时间:2013-05-10 09:23:48

标签: json facebook facebook-graph-api

我使用Facebook连接API从Facebook上的帐户中检索用户基本数据,我从java appliation连接到servlet:

private void getUserMailAddressFromJsonResponse(String accessToken,
        HttpSession httpSession) {
    String email = null;
    HttpClient httpclient = new DefaultHttpClient();
    try {
        if (accessToken != null && !"".equals(accessToken)) {
            String newUrl = "https://graph.facebook.com/me?access_token="
                    + accessToken;
            httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(newUrl);
            System.out.println("Get info from face --> executing request: "
                    + httpget.getURI());
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpget,
                    responseHandler);
            JSONObject json = (JSONObject) JSONSerializer
                    .toJSON(responseBody);
            System.out.println("Attributes inside Json "+json.toString());
            String facebookId = json.getString("id");
            String firstName = json.getString("first_name");
            String lastName = json.getString("last_name");
            email= json.getString("email");  // not founding email

            // put user data in session
            httpSession.setAttribute("FACEBOOK_USER", firstName + ","+lastName+","+email+","+facebookId);

        } else {
            System.err.println("Token za facebook je null");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
    // return email;
}

例外:

net.sf.json.JSONException: JSONObject["email"] not found

这是托管bean中形成URL的方法:

public String getFacebookUrlAuth() {
    System.out.println("Called");
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
            .getExternalContext().getSession(false);
    String sessionId = session.getId();
    System.out.println("SessionId:" + sessionId);
    String appId = "3607596261352927";
    String redirectUrl = "http://localhost:8080/Gambak/index.sec";
    String returnValue = "https://www.facebook.com/dialog/oauth?client_id="
            + appId + "&redirect_uri=" + redirectUrl
            + "&scope=email,user_birthday&state=" + sessionId;
    System.out.println("return value: " + returnValue);
    return returnValue;
}

奇怪的是,我3天前成功收到了电子邮件并且运行良好,当我今天尝试它时,我得到了例外!!

1 个答案:

答案 0 :(得分:1)

" Note, even if you request the email permission it is not guaranteed you will get an email address. For example, if someone signed up for Facebook with a phone number instead of an email address, the email field may be empty. " https://developers.facebook.com/docs/facebook-login/permissions/v2.5#reference-email
相关问题