使用 REST 和 HttpClient 访问 Facebook API

发布于 2024-08-04 07:05:59 字数 1140 浏览 6 评论 0原文

我将使用 RESTful Web 服务和 HttpClient 访问 Facebook API REST 服务器

我是 REST 和 Facebook API 的新手...

问题:

验证/授权

(1) 如果我有客户端应用程序发送的会话密钥,我如何验证和验证该密钥用户存在然后在服务器端查询他/她的好友?

我如何访问这些 Facebook RESTful 端点:

http://wiki.developers .facebook.com/index.php/Users.getInfo

http ://wiki.developers.facebook.com/index.php/Friends.getLists

通过 HTTP GET 请求访问 ?意思是,包含参数的完整 URL 是什么样的?

(2) 获取 API(我已在上面列出)的完整 RESTful URL 是什么样子?

发帖到朋友墙

(3) 验证/授权后,查询用户好友,如何(使用哪个API)发帖到朋友墙?

(4) 我是否需要附加到 Facebook RESTful 服务器 URL 的任何其他参数?

HTTP 客户端

(5) 我是否通过 HttpClient 在我的 Java 程序中包含对这些 Facebook API 的 RESTful Web 服务调用?

祝您编程愉快,感谢您花时间阅读本文...

I am going to use RESTful Web Services and HttpClient to access Facebook API REST Server.

Am somewhat of a newbie to REST and Facebook APIs...

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?

How can I be access these Facebook RESTful end points:

http://wiki.developers.facebook.com/index.php/Users.getInfo

and

http://wiki.developers.facebook.com/index.php/Friends.getLists

via a HTTP GET Request? Meaning, what does the full URL look like including parameters?

(2) What would the full RESTful URL look like to grab the APIs (which I have listed above)?

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?

(4) Is there any additional parameters that I need to append to the Facebook RESTful Server's URL?

HTTP Client

(5) Do I include the RESTful web service calls to these Facebook APIs inside my Java program through HttpClient?

Happy programming and thank you for taking the time to read this...

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

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

发布评论

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

评论(2

春花秋月 2024-08-11 07:05:59

我无法回答您所有的问题,但方法调用是通过 http://api.facebook.com/restserver.php 进行的,因此对 users.getInfo 的调用看起来像这样

http://api.facebook.com/restserver.php?method=users.getinfo

您还需要传入您的 api 密钥以及该方法所需的任何其他参数。但是,您不必自己进行 http 调用,而必须有一些 Java 库为您抽象出所有这些内容。

至于这是一种 REST API,有一个 Web 服务端点,其方法范围位于 URL 中,并且所有调用都是通过 HTTP GET 或 POST 进行的。

坦率地说,这是基于 HTTP 的 RPC,与 REST 相差甚远(没有双关语!)。 Facebook 应该更改他们的 API 文档,这完全是错误的。

I can't answer all your questions but the method calls are made via http://api.facebook.com/restserver.php so a call to users.getInfo looks like this

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.

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.

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.

清秋悲枫 2024-08-11 07:05:59

在创建 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("%1$032X", i).toLowerCase();
    }
    catch(NoSuchAlgorithmException nsae){ return ""; }
}
}

确保您看到关键组件 - 事实上参数是按字母顺序排序的,并且整个内容都是使用 MD5 加密的,但是加密的字符串与 URL 字符串略有不同。

另请注意,API 密钥需要填写!

因此,要获取 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);
}

希望这有帮助:)

In terms of creating the URL, I've used this code which seems to work pretty well...

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("%1$032X", 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.

Also note that the API keys need to be filled in!

So, to get the URL for the method User.getInfo and return the first and last names, I'd do the following...

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文