java 使用 REST 和 HttpClient 访问 Facebook API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1404028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access to Facebook API using REST and HttpClient
提问by mw_javaguy
I am going to use RESTful Web Services and HttpClientto access Facebook API REST Server.
我将使用 RESTful Web Services 和HttpClient来访问Facebook API REST Server。
Am somewhat of a newbie to REST and Facebook APIs...
我是 REST 和 Facebook API 的新手……
Question(s):
问题):
Verification / Authorization
验证/授权
(1) If I have a session key sent by a client app, how do I verify and authenticate that the user exists and then query for his / her friends on the server side?
(1)如果我有一个客户端应用发送的会话密钥,我如何验证和验证用户是否存在,然后在服务器端查询他/她的朋友?
How can I be access these Facebook RESTful end points:
我如何访问这些 Facebook RESTful 端点:
http://wiki.developers.facebook.com/index.php/Users.getInfo
http://wiki.developers.facebook.com/index.php/Users.getInfo
and
和
http://wiki.developers.facebook.com/index.php/Friends.getLists
http://wiki.developers.facebook.com/index.php/Friends.getLists
via a HTTP GET Request? Meaning, what does the full URL look like including parameters?
通过 HTTP GET 请求?意思是,包含参数的完整 URL 是什么样的?
(2) What would the full RESTful URL look like to grab the APIs (which I have listed above)?
(2) 获取 API(我已在上面列出)的完整 RESTful URL 是什么样的?
Posting to a Friend's Wall
发布到朋友的墙上
(3) After verification / authorization, querying users friends, how (which API) would I use to a post to a Friend's Wall?
(3) 验证/授权后,查询用户好友,我将如何(使用哪个API)发布到好友墙?
(4) Is there any additional parameters that I need to append to the Facebook RESTful Server's URL?
(4) 是否需要将任何其他参数附加到 Facebook RESTful 服务器的 URL?
HTTP Client
HTTP 客户端
(5) Do I include the RESTful web service calls to these Facebook APIs inside my Java program through HttpClient?
(5) 我是否通过 HttpClient 在我的 Java 程序中包含对这些 Facebook API 的 RESTful Web 服务调用?
Happy programming and thank you for taking the time to read this...
编程愉快,感谢您花时间阅读本文...
回答by blank
I can't answer all your questions but the method calls are made via http://api.facebook.com/restserver.phpso a call to users.getInfolooks like this
我不能回答你所有的问题,但该方法调用通过由http://api.facebook.com/restserver.php这样调用users.getInfo看起来像这样
http://api.facebook.com/restserver.php?method=users.getinfo
http://api.facebook.com/restserver.php?method=users.getinfo
You also need to pass in your api key and any other parameters the method needs. But rather than make the http calls yourself there must be some Java library that abstracts all this away for you.
您还需要传入您的 api 密钥和该方法需要的任何其他参数。但是,与其自己进行 http 调用,不如有一些 Java 库为您抽象出所有这些。
As for this being a REST API - there's one web service endpoint with method scoping in the URL and all calls are made via HTTP GET or POST.
至于这是一个 REST API - 在 URL 中有一个带有方法范围的 Web 服务端点,所有调用都是通过 HTTP GET 或 POST 进行的。
Frankly, this is RPC over HTTP and about as far from REST as you can get (no pun intended!). Facebook should change their API documentation, it's just plain wrong.
坦率地说,这是基于 HTTP 的 RPC,并且与您所能获得的 REST 相差甚远(没有双关语!)。Facebook 应该更改他们的 API 文档,这完全是错误的。
回答by Spedge
In terms of creating the URL, I've used this code which seems to work pretty well...
在创建 URL 方面,我使用了这段代码,它似乎工作得很好......
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
// Written by Stuart Davidson, www.spedge.com
public class JSONComm
{
private final String JSON_URL = "http://api.facebook.com/restserver.php";
private final String fbSecretKey = "xxx";
private final String fbApiKey = "xxx";
private final String fbApiId = "xxx";
private int callId = 0;
public int getNextCall() { callId++; return callId; }
public String getApiKey() { return fbApiKey; }
public String getApiId() { return fbApiId; }
public String getRestURL(HashMap<String, String> args)
{
String url = JSON_URL + "?";
for(String arg : args.keySet()) { url = url + arg + "=" + args.get(arg) + "&"; }
String sig = getMD5Hash(args);
url = url + "sig=" + sig;
return url;
}
public String getMD5Hash(HashMap<String, String> args)
{
String message = "";
Vector<String> v = new Vector<String>(args.keySet());
Collections.sort(v);
Iterator<String> it = v.iterator();
while(it.hasNext())
{
String tmp = it.next();
message = message + tmp + "=" + args.get(tmp);
}
message = message + fbSecretKey;
try{
MessageDigest m = MessageDigest.getInstance("MD5");
byte[] data = message.getBytes();
m.update(data,0,data.length);
BigInteger i = new BigInteger(1,m.digest());
return String.format("%12X", i).toLowerCase();
}
catch(NoSuchAlgorithmException nsae){ return ""; }
}
}
Make sure you see the critical components - the fact that the arguments are alphabetically sorted, and that the whole thing is encrypted using MD5, but the string that is encrypted is slightly different than the URL string.
确保您看到关键组件 - 参数按字母顺序排序,并且整个内容使用 MD5 加密,但加密的字符串与 URL 字符串略有不同。
Also note that the API keys need to be filled in!
还要注意API key需要填写!
So, to get the URL for the method User.getInfo and return the first and last names, I'd do the following...
因此,要获取 User.getInfo 方法的 URL 并返回名字和姓氏,我将执行以下操作...
public String getFbURL(String callback, Long playerId)
{
HashMap<String, String> args = new HashMap<String, String>();
args.put("api_key", jsonComm.getApiKey());
args.put("call_id", "" + jsonComm.getNextCall());
args.put("v", "1.0");
args.put("uids", "" + playerId);
args.put("fields", "first_name,last_name");
args.put("format", "JSON");
args.put("method", "Users.getInfo");
args.put("callback", "" + callback);
return jsonComm.getRestURL(args);
}
Hope this helps :)
希望这可以帮助 :)

