使用Lua使用Facebook Connect?

时间:2011-09-21 18:59:03

标签: facebook lua

我是Lua的初学者,并尝试使用Lua实现Facebook连接api。我在网上搜索并发现Corona SDK提供了这个,但我不允许购买任何SDK并使用Lua。    任何人都可以建议任何开源SDK或任何其他方式,我可以使用Lua实现facebook连接api?

2 个答案:

答案 0 :(得分:3)

首先,我将开始并说我不知道​​Lua是什么。也就是说,任何具有发出http请求能力的语言都可以使用facebook api。 Facebook's documentation of the Graph API详细说明了查询的内容和位置。

正如我想象的那样Lua and networking上有很多资源。对facebook的实际调用看起来像这样:

https://graph.facebook.com/oauth/access_token?
 client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&
 client_secret=YOUR_APP_SECRET

使用该代码检索您的访问令牌,然后您可以发出如下请求:

https://graph.facebook.com/SOME_USER_ID/feed?access_token=YOUR_ACCESS_TOKEN

这将为您提供用户墙及其数据的JSON响应。

开始玩这些网址及其含义的好地方是来自facebook的this great tool。它是Graph API Explorer。点击get access token并标记您需要的权限,然后点击您的所有Facebook数据!

答案 1 :(得分:1)

在Lua中可能还没有包装FB API。也就是说,这应该不难做到。

LuaSocket模块提供对HTTP请求的支持。您可以使用它来请求组成API调用的各个URL。这些请求将返回JSON格式的数据,您需要解析这些数据才能使用。

要在Lua中轻松解析JSON,您需要为JSON解析器找到合适的包装器。快速搜索显示有相当数量的选择。一个流行的似乎是JSON4Lua

Here是一篇文章,其中包含一个用于从Yahoo!访问特定基于JSON的API的工作示例基于LuaSocket和JSON4Lua。示例代码如下所示:

-- Client for the Yahoo Traffic API (http://developer.yahoo.com/traffic/rest/V1/index.html)
-- using JSON and Lua
-- Matt Croydon (matt@ooiio.com) http://postneo.com

http = require("socket.http") -- http://www.cs.princeton.edu/~diego/professional/luasocket/
json = require("json") -- http://json.luaforge.net/

-- Retrieve traffic information for Kansas City, MO
r, c, h = http.request("http://local.yahooapis.com/MapsService/V1/trafficData?appid=LuaDemo&city=Kansas+City&state=MO&output=json")

if c == 200 then
    -- Process the response
    results = json.decode(r)["ResultSet"]["Result"]
    -- Iterate over the results
    for i=1,table.getn(results) do
        print("Result "..i..":")
        table.foreach(results[i], print)
        print()
    end
end