Lua Requires Chilkat v11.0.0+
Lua
Get the Photos for a User
See more Facebook Examples
Demonstrates how to get the photos that the user has uploaded.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- This example assumes a previously obtained an access token
local oauth2 = chilkat.OAuth2.new()
oauth2.access_token = "FACEBOOK-ACCESS-TOKEN"
local rest = chilkat.Rest.new()
-- Connect to Facebook.
local success = rest:connect("graph.facebook.com", 443, true, true)
if not success then
print(rest.last_error_text)
return
end
-- Provide the authentication credentials (i.e. the access key)
rest:set_auth_oauth2(oauth2)
-- Indicate that we only want the photos the user has personally uploaded.
rest:add_query_param("type", "uploaded")
-- We could limit the number of photos by setting a limit.
rest:add_query_param("limit", "5")
-- Gets the 1st page of photos. (Not the actual image data, but the information about each photo.)
-- See https://developers.facebook.com/docs/graph-api/reference/user/photos/ for more information.
local responseJson = rest:full_request_no_body("GET", "/v2.7/me/photos")
if not rest.last_method_success then
print(rest.last_error_text)
return
end
local json = chilkat.JsonObject.new()
json.emit_compact = false
json:load(responseJson)
print(json:emit())
-- A sample JSON response is shown below.
-- This is the code to parse the JSON response.
local dtime = chilkat.DateTime.new()
local bLocalTime = true
local dt = chilkat.DtObj.new()
local i = 0
local numItems = json:size_of_array("data")
while i < numItems do
json.i = i
print("--- " .. i)
local name = json:string_of("data[i].name")
if json.last_method_success then
print("name: " .. name)
end
print("id: " .. json:string_of("data[i].id"))
-- We can load the created_time into a CkDateTime...
dtime:set_from_timestamp(json:string_of("data[i].created_time"))
dtime:to_dt_obj(bLocalTime, dt)
print(dt.month .. "/" .. dt.day .. "/" .. dt.year .. " " .. dt.hour .. ":" .. dt.minute)
i = i + 1
end
-- We can get the paging information as follows:
print("URL for next page: " .. json:string_of("paging.next"))
print("before cursor: " .. json:string_of("paging.cursors.before"))
print("after cursor: " .. json:string_of("paging.cursors.after"))
-- This is a sample JSON response:
-- {
-- "data": [
-- {
-- "created_time": "2016-09-29T20:46:18+0000",
-- "name": "Ignore my posts -- I'm doing some testing for Facebook related programming...",
-- "id": "10210199026347451"
-- },
-- {
-- "created_time": "2016-09-19T02:00:42+0000",
-- "id": "10210091531240138"
-- },
-- {
-- "created_time": "2016-09-19T02:00:42+0000",
-- "id": "10210091520620125"
-- },
-- {
-- "created_time": "2016-09-19T01:59:46+0000",
-- "name": "I would've went for a swim had it not been for the sign",
-- "id": "10210091522299917"
-- },
-- {
-- "created_time": "2016-09-12T00:37:35+0000",
-- "id": "10210023316834798"
-- }
-- ],
-- "paging": {
-- "cursors": {
-- "before": "MTAyMTAxOTkwMjYzNDc0NTEZD",
-- "after": "MTAyMTAwMjMzMTU4MzQ3OTgZD"
-- },
-- "next": "https:\/\/graph.facebook.com\/v2.7\/10224048320139890\/photos?type=uploaded&limit=5&after=MTAyMTAwMjMzMTU4MzQ3OTgZD"
-- }
-- }
--