Sample code for 30+ languages & platforms
Rust

Paging User Photos with Cursor

See more Facebook Examples

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

Chilkat Rust Downloads

Rust

// 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
let oauth2 = chilkat::OAuth2::new();
oauth2.set_access_token("FACEBOOK-ACCESS-TOKEN");

let rest = chilkat::Rest::new();

// Connect to Facebook.
if rest.connect("graph.facebook.com", 443, true, true).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// Provide the authentication credentials (i.e. the access key)
let _ = rest.set_auth_o_auth2(&oauth2);

// Indicate that we only want the photos the user has personally uploaded.
let _ = rest.add_query_param("type", "uploaded");

// We could limit the number of photos per page using the "limit" field.
let _ = 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.
let Ok(mut response_json) = rest.full_request_no_body("GET", "/v2.7/me/photos") else {
    println!("{}", rest.last_error_text());
    return;
};

let json = chilkat::JsonObject::new();
json.set_emit_compact(false);
let _ = json.load(&response_json);
println!("{}", json.emit().unwrap_or_default());

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

// Get the "after" cursor.
let mut after_cursor = json.string_of("paging.cursors.after").unwrap_or_default();
while json.last_method_success() {

    println!("after cursor: {}", after_cursor);

    // 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.
    let _ = rest.clear_all_query_params();
    let _ = rest.add_query_param("type", "uploaded");
    let _ = rest.add_query_param("limit", "20");
    let _ = rest.add_query_param("after", &after_cursor);

    response_json = rest.full_request_no_body("GET", "/v2.7/me/photos").unwrap_or_default();
    if !rest.last_method_success() {
        println!("{}", rest.last_error_text());
        return;
    }

    let _ = json.load(&response_json);
    // See Parsing the Facebook User Photos for code showing how to parse the JSON photos content.

    println!("{}", json.emit().unwrap_or_default());

    // Get the cursor for the next page.
    after_cursor = json.string_of("paging.cursors.after").unwrap_or_default();
}

println!("No more pages of photos.");