如何从 Servlet 使用 Facebook Graph-API

发布于 2024-08-30 22:44:01 字数 290 浏览 3 评论 0原文

我需要从 J2EE 应用程序(服务器端)访问 Facebook。 我首先看了一下这个项目: http://code.google.com/ p/facebook-java-api/ ,但由于我需要创建 Facebook 活动并邀请人们,这没有帮助。

所以我想我需要使用 Graph API,但我不知道如何执行所需的 HTTP POST 请求 - 特别是如何附加 nedded 属性。

I need to access Facebook from a J2EE-App (serverside).
I first had a look at this project: http://code.google.com/p/facebook-java-api/
, but as I need to create Facebook-Events and invite people this does not help.

So I guess I need to use the Graph API, but Igot no clue on how to perform those HTTP POST requests needed - especially how to append the nedded attributes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尬尬 2024-09-06 22:44:01

您可以使用 java.net.URLConnection 为此:

String url = "http://facebook.com/some/api";
String charset = "UTF-8";
String param1 = URLEncoder.encode("value1", charset);
String param2 = URLEncoder.encode("value2", charset);
String query = String.format("param1=%s¶m2=%s", param1, param2);

URLConnection urlConnection = new URL(url).openConnection();
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true); // Triggers POST.
urlConnection.setRequestProperty("accept-charset", charset);
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");

OutputStreamWriter writer = null;
try {
    writer = new OutputStreamWriter(urlConnection.getOutputStream(), charset);
    writer.write(query); // Write POST query string (if any needed).
} finally {
    if (writer != null) try { writer.close(); } catch (IOException logOrIgnore) {}
}

InputStream response = urlConnection.getInputStream();
// Now do your thing with the facebook response.

或者,您也可以使用更方便的 HttpClient API 为此:

String url = "http://facebook.com/some/api";
String charset = "UTF-8";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1", "value1"));
params.add(new BasicNameValuePair("param2", "value2"));
UrlEncodedFormEntity query = new UrlEncodedFormEntity(params, charset);

HttpClient client = new DefaultHttpClient()
HttpPost post = new HttpPost(url);
post.setEntity(query);
InputStream response = client.execute(post).getEntity().getContent();
// Now do your thing with the facebook response.

You can use the java.net.URLConnection for this:

String url = "http://facebook.com/some/api";
String charset = "UTF-8";
String param1 = URLEncoder.encode("value1", charset);
String param2 = URLEncoder.encode("value2", charset);
String query = String.format("param1=%s¶m2=%s", param1, param2);

URLConnection urlConnection = new URL(url).openConnection();
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true); // Triggers POST.
urlConnection.setRequestProperty("accept-charset", charset);
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");

OutputStreamWriter writer = null;
try {
    writer = new OutputStreamWriter(urlConnection.getOutputStream(), charset);
    writer.write(query); // Write POST query string (if any needed).
} finally {
    if (writer != null) try { writer.close(); } catch (IOException logOrIgnore) {}
}

InputStream response = urlConnection.getInputStream();
// Now do your thing with the facebook response.

Alternatively, you can also use the more convenienced HttpClient API for this:

String url = "http://facebook.com/some/api";
String charset = "UTF-8";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1", "value1"));
params.add(new BasicNameValuePair("param2", "value2"));
UrlEncodedFormEntity query = new UrlEncodedFormEntity(params, charset);

HttpClient client = new DefaultHttpClient()
HttpPost post = new HttpPost(url);
post.setEntity(query);
InputStream response = client.execute(post).getEntity().getContent();
// Now do your thing with the facebook response.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文