JSON没有得到电子邮件
问题描述:
我用Facebook的,通过连接的servlet API从他的Facebook帐户检索用户的基本数据,我从Java appliation连接:JSON没有得到电子邮件
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天前的电子邮件,它运行良好,当我今天尝试它时,我得到了这个异常!
答
“请注意,即使您要求获得电子邮件许可,也无法保证您会收到电子邮件地址。例如,如果某人使用电话号码而不是电子邮件地址注册Facebook,则电子邮件字段可能是空。“
https://developers.facebook.com/docs/facebook-login/permissions/v2.5#reference-email
你有[电子邮件许可(https://developers.facebook.com/docs/reference/login/email-permissions/)? – 2013-05-10 10:46:30
String returnValue =“https://www.facebook.com/dialog/oauth?client_id=” + appId +“&redirect_uri =”+ redirectUrl +“&scope = email,user_birthday&state =”+ sessionId; – 2013-05-10 11:23:49