I'm trying to integrate facebook login into my webapp, and I want to get the email from the user, but I can only get the ID and name all the time, please give me some tips if you know how to fixed it.
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@page import="java.net.URLEncoder" %>
<%
String fbURL = "http://www.facebook.com/dialog/oauth?client_id=facebookAppID&redirect_uri=" + URLEncoder.encode("http://localhost:8080/TestFacebook/signin_fb.do?")+"&scope=email";
%>
<a href="<%= fbURL %>"><img src="img/facebookloginbutton.png" border="0" /></a>
</body>
</html>
Servlet
package login;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;
@WebServlet("/signin_fb.do")
public class SignInFB extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String code = req.getParameter("code");
if (code == null || code.equals("")) {
// an error occurred, handle this
throw new RuntimeException(
"ERROR: Didn't get code parameter in callback.");
}
String token = null;
try {
String g = "https://graph.facebook.com/oauth/access_token?client_id=facebookAppID&redirect_uri=" + URLEncoder.encode("http://localhost:8080/TestFacebook/signin_fb.do", "UTF-8") + "&client_secret=AppSecret&code=" + code;
URL u = new URL(g);
URLConnection c = u.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
String inputLine;
StringBuffer b = new StringBuffer();
while ((inputLine = in.readLine()) != null)
b.append(inputLine + "\n");
in.close();
token = b.toString();
System.out.println("token= "+token);
if (token.startsWith("{"))
throw new Exception("error on requesting token: " + token + " with code: " + code);
} catch (Exception e) {
// an error occurred, handle this
throw new RuntimeException("ERROR: Access Token Invalid: "
+ token);
}
String graph = null;
try {
String g = "https://graph.facebook.com/me?"+token;
URL u = new URL(g);
URLConnection c = u.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
String inputLine;
StringBuffer b = new StringBuffer();
while ((inputLine = in.readLine()) != null)
b.append(inputLine + "\n");
in.close();
graph = b.toString();
System.out.println("graph= " +graph);
} catch (Exception e) {
// an error occurred, handle this
e.printStackTrace();
throw new RuntimeException("ERROR in getting FB graph data. " + e);
}
String facebookId;
String firstName;
String middleNames;
String lastName;
String email;
try {
JSONObject json = new JSONObject(graph);
facebookId = json.getString("id");
firstName = json.getString("first_name");
lastName = json.getString("last_name");
email = json.getString("email");
ServletOutputStream out = resp.getOutputStream();
out.print("<div>Welcome "+firstName+" "+lastName);
out.print("<div>Email: "+email);
System.out.println("json= "+json);
} catch (JSONException e) {
// an error occurred, handle this
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
I want to print out the status of user, but the only thing I get is graph= {"name":"XXX","id":"xxxxxxxxxxxxxxxx"}, Please let me know if you have any way to fix it, appreciate for any help.