在我的网站上,用户可以使用Facebook登录。这样做时我会请求许可 发布到用户Feed。这就像一个魅力。
登录后,允许用户撰写评论,在保存评论时,系统会询问用户是否要将评论发布到Facebook上的用户Feed。由于在将评论保存在我的本地数据库中之后应该发布到facebook的帖子,我知道我需要执行身份验证服务器,然后当我有一个令牌时我能够POST到例如。
http://graph.facebook.com/10XXXX40308/feed
带
message : "This works"
我一直在尝试按照here所述的方式实施facebook web登录:
步骤如下:
在浏览器中执行1.时,应用程序会相应地运行。我使用MY_VERIFICATION_CODE:
从Facebook返回重定向
所以我尝试用这样的代码来做:
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");
但接下来发生的事情是,我得到200回来和登录,而不是让302回来 代码页:
似乎我错过了一步或者不了解流程。
我想要实现的是实现类似于janrain的类似调用:
https://rpxnow.com/api/v2/facebook/stream.publish
允许你这样做。
谢谢!
答案 0 :(得分:0)
我猜 rtfm 就在这里。用户需要进行身份验证,因此我真正想要做的就是绕过身份验证过程。这当然是不允许的。所以 你怎么解决这个?
当用户进行身份验证时,我需要保存访问令牌和到期时间 我可以稍后将其添加到请求中。
我认为这是唯一的方法......如果我错了,请纠正我。
因此,在身份验证过程中,我创建了一个正则表达式:
Pattern pattern = Pattern.compile("access_token=([a-zA-Z0-9]+)&expires=([0-9]+)");
然后在facebook的回调中,我使用regexp提取令牌:
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()));
}
如果您有更好的解决方案,请分享=)