Facebook HTTP.request "GET" (SOLVED)

Hi,

How would you go about getting the First Name from the Facebook profile using the http.request “GET” method.

I got the following from Defold’s Facebook API documentation:

local function get_name_callback(self, id, response)
-- do something with the response
end

function init(self)
-- assuming we are already logged in.
local token = facebook.access_token()
    if token then
       local url = "https://graph.facebook.com/me/?access_token=".. token
       http.request(url, "GET", get_name_callback)
    end
end

But how would you use the get_name_callback to get the First Name?

If you haven’'t already, try the example project.
Here’s that particular get_name_callback.

2 Likes

It works great but I get the full name and I only want the first name.

Then the Lua manual is your next stop, namely string.sub() and string.find():

local fullname = ...
local firstname = string.sub(fullname, 1, string.find(fullname, " ")-1)
print(firstname)
2 Likes

Thank you! Its working.

Alternative:

"https://graph.facebook.com/me/?access_token=" ..  token .. "&fields=id,first_name"

This also lets you request for additional fields you might need later in 1 query.

2 Likes