【问题标题】:Facebook oauth - How to handle response in javaFacebook oauth - 如何在 java 中处理响应
【发布时间】:2015-08-11 08:40:42
【问题描述】:

我正在遵循此答案中提到的步骤:https://stackoverflow.com/a/9580644/4950201

我成功注册了 Facebook 应用并从网页调用登录对话框(用户单击登录 Facebook,他登录,Facebook 将他重定向到附加代码的 redirect_uri)。

现在我不知道如何在服务器端使用以下 Java 代码(取自上面的答案):

import org.json.JSONObject;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SignInFB extends HttpServlet {

public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {            
    String code = req.getParameter("code");
    if (code == null || code.equals("")) {
        // an error occurred, handle this
    }

    String token = null;
    try {
        String g = "https://graph.facebook.com/oauth/access_token?client_id=myfacebookappid&redirect_uri=" + URLEncoder.encode("http://myappengineappid.appspot.com/signin_fb.do", "UTF-8") + "&client_secret=myfacebookappsecret&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();
        if (token.startsWith("{"))
            throw new Exception("error on requesting token: " + token + " with code: " + code);
    } catch (Exception e) {
            // an error occurred, handle this
    }

    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();
    } catch (Exception e) {
            // an error occurred, handle this
    }

    String facebookId;
    String firstName;
    String middleNames;
    String lastName;
    String email;
    Gender gender;
    try {
        JSONObject json = new JSONObject(graph);
        facebookId = json.getString("id");
        firstName = json.getString("first_name");
        if (json.has("middle_name"))
           middleNames = json.getString("middle_name");
        else
            middleNames = null;
        if (middleNames != null && middleNames.equals(""))
            middleNames = null;
        lastName = json.getString("last_name");
        email = json.getString("email");
        if (json.has("gender")) {
            String g = json.getString("gender");
            if (g.equalsIgnoreCase("female"))
                gender = Gender.FEMALE;
            else if (g.equalsIgnoreCase("male"))
                gender = Gender.MALE;
            else
                gender = Gender.UNKNOWN;
        } else {
            gender = Gender.UNKNOWN;
        }
    } catch (JSONException e) {
        // an error occurred, handle this
    }

    ...

当用户登录时,Facebook 将他重定向到redirect_uri,并且在redirect_uri 页面我必须从客户端调用服务器端函数服务(从脚本调用@service)?

如果是,那么我应该将哪些参数传递给服务函数? (获取如下参数(HttpServletRequest req, HttpServletResponse res))

我了解 Java 代码和获取访问令牌的过程,但是在 Facebook 将用户重定向到 redirect_uri 后,我不知道如何使用和调用上述函数。

如果有人可以帮助我,我会很高兴。

谢谢

【问题讨论】:

    标签: java facebook facebook-graph-api oauth facebook-oauth


    【解决方案1】:

    您不必从脚本中调用它。基本上,您的重定向 URI 本身应该是 @RequestMapping。在此请求映射中,您必须编写逻辑来捕获 Facebook 提供给您的 access_token(您看到的附加到提到的重定向 URI 的代码)并安全地存储它。

    与 FB 的所有进一步交互都应在控制器中编写为单独的 @RequestMapping。例如你可以有一个 @RequestMapping 说 ListFriends 你会列出你所有的朋友。

    此 ListFriends servlet 中的逻辑将使用访问令牌(在上一步中获得)与 Facebook API 通信,然后将列表返回到将列表呈现到网页的层。

    希望这会有所帮助。

    【讨论】:

    • servlet 基本上是一个java 文件吧?我问是因为我的项目处于模型控制器视图架构中,并且每个页面都是 html 中的常规页面,并且在页面中我调用服务器。那么在这种情况下 uri 本身如何成为一个 servlet 呢?谢谢:-)
    • 在 localhost 链接上测试这个有什么问题吗?
    • @02416 一开始我并不知道你在使用 MVC。假设您使用的是 Spring MVC 框架,更新了我的答案
    • @02416 我认为使用 localhost 链接应该没有任何问题。但我不太确定。通常我会在 Heroku 或其他托管服务上托管我的应用程序并提供该 URI。
    • 好的,谢谢。关于 uri 本身应该是 @RequestMapping ,这不会导致错误,因为在 Facebook 上的应用程序设置中有一个常规链接? (http://...)
    猜你喜欢
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    相关资源
    最近更新 更多