2

我使用 Facebook 连接 API 从他在 facebook 上的帐户中检索用户基本数据,我通过 servlet 从 java 应用程序连接:

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

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

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 天前成功收到了电子邮件,并且运行良好,当我今天尝试时,我得到了那个异常!

4

1 回答 1

1

"请注意,即使您请求电子邮件许可,也不能保证您会获得电子邮件地址。例如,如果有人使用电话号码而不是电子邮件地址注册 Facebook,则电子邮件字段可能为空。 "

https://developers.facebook.com/docs/facebook-login/permissions/v2.5#reference-email

于 2015-10-30T23:39:20.593 回答