Sample code for 30+ languages & platforms
Lua

Paging User Photos with Cursor

See more Facebook Examples

Demonstrates how to iterate over the pages of user photos using a cursor.

Chilkat Lua Downloads

Lua
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 per page using the "limit" field.
rest:add_query_param("limit", "20")

-- Get 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())

-- See Parsing the Facebook User Photos for code showing how to parse the JSON photos content.
--

-- Get the "after" cursor.
local afterCursor = json:string_of("paging.cursors.after")
while json.last_method_success do
    print("after cursor: " .. afterCursor)

    -- Prepare for getting the next page of photos.
    -- We can continue using the same REST object.
    -- If already connected, we'll continue using the existing connection.
    -- Otherwise, a new connection will automatically be made if needed.
    rest:clear_all_query_params()
    rest:add_query_param("type", "uploaded")
    rest:add_query_param("limit", "20")
    rest:add_query_param("after", afterCursor)

    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

    json:load(responseJson)
    -- See Parsing the Facebook User Photos for code showing how to parse the JSON photos content.

    print(json:emit())

    -- Get the cursor for the next page.
    afterCursor = json:string_of("paging.cursors.after")
end

print("No more pages of photos.")