如何在不使用 JS 库的情况下通过 facebook 服务器端进行身份验证?

问题描述 投票:0回答:1

在我的网站上,用户可以使用 Facebook 登录。这样做时我会请求许可 发布到用户提要。这就像一个魅力。

登录后,用户可以撰写评论,保存评论时,系统会询问用户是否要将评论发布到 Facebook 上的用户源。由于发布到 Facebook 的操作应该在评论保存在我的本地数据库之后完成,所以我知道我需要在服务器端执行身份验证,然后当我拥有令牌时,我就可以发布到例如。

http://graph.facebook.com/10XXXX40308/feed 

message : "This works"

我一直在尝试实现 facebook 网络登录,如here:

所述

步骤是:

  1. 执行请求
    https://graph.facebook.com/oauth/authorize?client_id=MY_API_KEY&
    redirect_uri=http://www.facebook.com/connect/login_success.html&
    范围=publish_stream
  2. Facebook 会将您重定向至 http://www.facebook.com/connect/login_success.html? 代码=MY_VERIFICATION_CODE
  3. 请求 https://graph.facebook.com/oauth/access_token?client_id=MY_API_KEY& redirect_uri=http://www.facebook.com/connect/login_success.html& client_secret=MY_APP_SECRET&code=MY_VERIFICATION_CODE Facebook 将 使用 access_token=MY_ACCESS_TOKEN 进行响应

在浏览器中执行 1. 操作时,应用程序会做出相应的行为。我从 Facebook 收到带有 MY_VERIFICATION_CODE 的重定向:

success

所以我尝试用这样的代码来做:

        String url = "https://graph.facebook.com/oauth/authorize?client_id="+clientId+"&scope=publish_stream&redirect_uri=http://www.facebook.com/connect/login_success.html";

        URL obj = new URL(url);
        conn = (HttpURLConnection) obj.openConnection();
        conn.setReadTimeout(5000);
        conn.setRequestMethod("GET");

        System.out.println("Request URL ... " + url);

        boolean redirect = false;

        // normally, 3xx is redirect
        int status = conn.getResponseCode();
        if (status != HttpURLConnection.HTTP_OK) {
            if (status == HttpURLConnection.HTTP_MOVED_TEMP
                    || status == HttpURLConnection.HTTP_MOVED_PERM
                    || status == HttpURLConnection.HTTP_SEE_OTHER)
                redirect = true;
        }

        System.out.println("Response Code ... " + status);

        if (redirect) {

            // get redirect url from "location" header field
            String newUrl = conn.getHeaderField("Location");

            // get the cookie if need, for login
            String cookies = conn.getHeaderField("Set-Cookie");

            // open the new connnection again
            conn = (HttpURLConnection) new URL(newUrl).openConnection();
            conn.setRequestProperty("Cookie", cookies);                

            System.out.println("Redirect to URL : " + newUrl);

        }

        BufferedReader in = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer html = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            html.append(inputLine);
        }
        in.close();

        System.out.println("URL Content... \n" + html.toString());
        System.out.println("Done");

但是发生的情况是,我没有得到 302,而是得到了 200 和登录信息 代码中的页面:

enter image description here

我好像漏了一步或者没看懂流程。

我想要完成的是实现与 janrain 类似的调用:

https://rpxnow.com/api/v2/facebook/stream.publish

您可以这样做的地方。

java facebook-graph-api facebook-opengraph
1个回答
0
投票

我猜rtfm就在这里。用户需要进行身份验证,所以我在这里真正想做的是绕过身份验证过程。这当然是不允许的。所以 你怎么解决这个问题?

当用户进行身份验证时,我需要保存访问令牌和过期时间,以便 我可以稍后将其添加到请求中。

我认为这是唯一的方法。

因此在身份验证过程中我创建了一个正则表达式:

Pattern pattern = Pattern.compile("access_token=([a-zA-Z0-9]+)&expires=([0-9]+)"); 

然后在 Facebook 的回调中,我使用正则表达式提取令牌:

    String accessToken = "";
    String expires = "";

    Matcher matcher = pattern.matcher(token.trim());
    if(matcher.matches()) {
        accessToken = matcher.group(1);
        expires = matcher.group(2);
    } else {
        return new JSONObject()
                .put("error", "OathBean: accessToken is null");
    }

然后我调用 facebook 来获取用户的值并返回所有值,以便我可以使用它们:

return new JSONObject()
                .put("facebookId", facebookId)
                .put("firstName", firstName)
                .put("lastName", lastName)
                .put("email", email)
                .put("photo", photo)
                .put("accessToken", accessToken)
                .put("expires", expires);

稍后当用户想要在 Facebook 上发布评论时。我填写请求并发布评论。

            Map<String, String> requestData = new HashMap<String, String>();
            requestData.put("link",url(newReview));                
            requestData.put("description", "reviewText");
            requestData.put("access_token", credential.getPassword());
            String query = createQuery(requestData);
            JSONObject result = null;
            try {
                URL url = new URL("https://graph.facebook.com/"+identifier+"/feed");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);

                conn.connect();
                OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
                osw.write(query);
                osw.close();

                result =  new JSONObject(IOUtils.toString(conn.getInputStream()));
            }
            catch (Exception    e) {
                log.error("Could not call graph feed to publish for id: "+identifier, e);
            }

            if(result != null) {
                boolean success = StringUtils.isNotBlank(result.getString("id"));
                entityManager.persist(new FBPublishEvent(currentUser, newReview, success, result.toString()));                    
            }
© www.soinside.com 2019 - 2024. All rights reserved.