2013-05-10 187 views
2

我用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天前的電子郵件,它運行良好,當我今天嘗試它時,我得到了這個異常!

+1

你有[電子郵件許可(https://developers.facebook.com/docs/reference/login/email-permissions/)? – 2013-05-10 10:46:30

+1

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

回答

相關問題