3

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 を使用して Friend's Wall に投稿しますか?

(4) Facebook RESTful サーバーの URL に追加する必要がある追加のパラメーターはありますか?

HTTP クライアント

(5) HttpClient を介して Java プログラム内にこれらの Facebook API への RESTful Web サービス呼び出しを含めますか?

楽しいプログラミングと、これを読んでくれてありがとう...

4

2 に答える 2

1

すべての質問にお答えすることはできませんが、メソッド呼び出しは経由で行われるhttp://api.facebook.com/restserver.phpため、への呼び出しはusers.getInfo次のようになります

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

また、API キーと、メソッドに必要なその他のパラメーターを渡す必要があります。しかし、自分で http 呼び出しを行うのではなく、これらすべてを抽象化する Java ライブラリーが必要です。

これは REST API であるため、URL にメソッド スコープを持つ 1 つの Web サービス エンドポイントがあり、すべての呼び出しは HTTP GET または POST を介して行われます。

率直に言って、これは RPC over HTTP であり、REST とはかけ離れたものです (しゃれは意図していません!)。Facebook は API ドキュメントを変更する必要がありますが、それは明らかに間違っています。

于 2009-11-17T07:53:16.207 に答える
0

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

于 2010-03-07T09:37:06.090 に答える