Facebook HTTP.request "GET" (SOLVED)

#1

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?

0 Likes

#2

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

2 Likes

#3

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

0 Likes

#4

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

#5

Thank you! Its working.

0 Likes

#6

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