我使用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天前成功收到了电子邮件并且运行良好,当我今天尝试它时,我得到了例外!!
答案 0 :(得分:1)