Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Get the Photos for a User

See more Facebook Examples

Demonstrates how to get the photos that the user has uploaded.

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 by setting a limit.
let _ = 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.
let Ok(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());

// A sample JSON response is shown below.  
// This is the code to parse the JSON response.

let dtime = chilkat::DateTime::new();
let b_local_time = true;

let dt = chilkat::DtObj::new();
let mut i = 0;
let num_items = json.size_of_array("data");
while i < num_items {
    json.set_i(i);
    println!("--- {}", i);
    if let Ok(name) = json.string_of("data[i].name") {
        println!("name: {}", name);
    }

    println!("id: {}", json.string_of("data[i].id").unwrap_or_default());

    // We can load the created_time into a CkDateTime...
    let _ = dtime.set_from_timestamp(&json.string_of("data[i].created_time").unwrap_or_default());
    dtime.to_dt_obj(b_local_time, &dt);

    println!("{}/{}/{}  {}:{}", dt.month(), dt.day(), dt.year(), dt.hour(), dt.minute());
    i = i + 1;
}

// We can get the paging information as follows:
println!("URL for next page: {}", json.string_of("paging.next").unwrap_or_default());
println!("before cursor: {}", json.string_of("paging.cursors.before").unwrap_or_default());
println!("after cursor: {}", json.string_of("paging.cursors.after").unwrap_or_default());

// 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"
//   }
// }
//