Facebook图表API无法收到电子邮件

时间:2016-12-26 22:55:13

标签: facebook facebook-graph-api

我试图将facebook登录集成到我的webapp中,我想收到用户发来的电子邮件,但我只能随时获取ID和名称,如果你知道怎么做,请给我一些提示解决它。

的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);
    }

}

我想打印出用户的状态,但我唯一得到的是 graph = {&#34; name&#34;:&#34; XXX&#34;,&#34; id&#34;:&#34; xxxxxxxxxxxxxxxx&#34;},如果您有任何办法,请告诉我解决它,感谢任何帮助。

0 个答案:

没有答案