Sample code for 30+ languages & platforms
Rust

Google People API - List the User's Contacts

See more Google People Examples

Gets a list of people in the user's contacts.

Chilkat Rust Downloads

Rust

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// It is assumed we previously obtained an OAuth2 access token.
// This example loads the JSON access token file 

// originally obtained by this example: Get Google People API OAuth2 Access Token
// or refreshed by this example: Refresh Google People API OAuth2 Access Token

let json_token = chilkat::JsonObject::new();
if json_token.load_file("qa_data/tokens/googlePeople.json").is_err() {
    println!("Failed to load googleContacts.json");
    return;
}

let http = chilkat::Http::new();

http.set_auth_token(&json_token.string_of("access_token").unwrap_or_default());

let sb_response_body = chilkat::StringBuilder::new();
if http.quick_get_sb("https://people.googleapis.com/v1/people/me/connections?personFields=names,addresses,emailAddresses", &sb_response_body).is_err() {
    println!("{}", http.last_error_text());
    println!("{}", sb_response_body.get_as_string().unwrap_or_default());
    return;
}

// Sample JSON Response

// Use this online tool to generate parsing code from sample JSON: 
// Generate Parsing Code from JSON

// {
//   "connections": [
//     {
//       "resourceName": "people/c8871101035606120608",
//       "etag": "%EgkBAj0JQhBANy4aBAECBQciDERLSGdVTFNPbzNJPQ==",
//       "names": [
//         {
//           "metadata": {
//             "primary": true,
//             "source": {
//               "type": "CONTACT",
//               "id": "7b1c7b6409e718a0"
//             }
//           },
//           "displayName": "Chilkat Cloud",
//           "familyName": "Cloud",
//           "givenName": "Chilkat",
//           "displayNameLastFirst": "Cloud, Chilkat",
//           "unstructuredName": "Chilkat Cloud"
//         }
//       ],
//       "emailAddresses": [
//         {
//           "metadata": {
//             "primary": true,
//             "source": {
//               "type": "CONTACT",
//               "id": "7b1c7b6409e718a0"
//             }
//           },
//           "value": "support@***.com"
//         }
//       ]
//     },
//     {
//       "resourceName": "people/c7607335470312011517",
//       "etag": "%EgkBAj0JQhBANy4aBAECBQciDGZETUtHVTVMazI4PQ==",
//       "names": [
//         {
//           "metadata": {
//             "primary": true,
//             "source": {
//               "type": "CONTACT",
//               "id": "6992af4e0b2d36fd"
//             }
//           },
//           "displayName": "Matt Smith",
//           "familyName": "Smith",
//           "givenName": "Matt",
//           "displayNameLastFirst": "Smith, Matt",
//           "unstructuredName": "Matt Smith"
//         }
//       ],
//       "addresses": [
//         {
//           "metadata": {
//             "primary": true,
//             "source": {
//               "type": "CONTACT",
//               "id": "6992af4e0b2d36fd"
//             }
//           },
//           "formattedValue": "2222 E Foorest Ave\nWheaton, IL 60999\nUS",
//           "streetAddress": "2222 E Foorest Ave",
//           "city": "Wheaton",
//           "region": "IL",
//           "postalCode": "60999",
//           "country": "US",
//           "countryCode": "US"
//         }
//       ],
//       "emailAddresses": [
//         {
//           "metadata": {
//             "primary": true,
//             "source": {
//               "type": "CONTACT",
//               "id": "6992af4e0b2d36fd"
//             }
//           },
//           "value": "matt@***.com"
//         },
//         {
//           "metadata": {
//             "source": {
//               "type": "CONTACT",
//               "id": "6992af4e0b2d36fd"
//             }
//           },
//           "value": "admin@***.com"
//         }
//       ]
//     }
//   ],
//   "totalPeople": 2,
//   "totalItems": 2
// }

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

let mut j: i32 = 0;
let mut count_j: i32 = 0;

let total_people = json.int_of("totalPeople");
let total_items = json.int_of("totalItems");
let mut i = 0;
let count_i = json.size_of_array("connections");
while i < count_i {
    json.set_i(i);
    let _ = json.string_of("connections[i].resourceName").unwrap_or_default();
    let _ = json.string_of("connections[i].etag").unwrap_or_default();
    j = 0;
    count_j = json.size_of_array("connections[i].names");
    while j < count_j {
        json.set_j(j);
        let _ = json.bool_of("connections[i].names[j].metadata.primary");
        let _ = json.string_of("connections[i].names[j].metadata.source.type").unwrap_or_default();
        let _ = json.string_of("connections[i].names[j].metadata.source.id").unwrap_or_default();
        let _ = json.string_of("connections[i].names[j].displayName").unwrap_or_default();
        let _ = json.string_of("connections[i].names[j].familyName").unwrap_or_default();
        let _ = json.string_of("connections[i].names[j].givenName").unwrap_or_default();
        let _ = json.string_of("connections[i].names[j].displayNameLastFirst").unwrap_or_default();
        let _ = json.string_of("connections[i].names[j].unstructuredName").unwrap_or_default();
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("connections[i].emailAddresses");
    while j < count_j {
        json.set_j(j);
        let _ = json.bool_of("connections[i].emailAddresses[j].metadata.primary");
        let _ = json.string_of("connections[i].emailAddresses[j].metadata.source.type").unwrap_or_default();
        let _ = json.string_of("connections[i].emailAddresses[j].metadata.source.id").unwrap_or_default();
        let _ = json.string_of("connections[i].emailAddresses[j].value").unwrap_or_default();
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("connections[i].addresses");
    while j < count_j {
        json.set_j(j);
        let _ = json.bool_of("connections[i].addresses[j].metadata.primary");
        let _ = json.string_of("connections[i].addresses[j].metadata.source.type").unwrap_or_default();
        let _ = json.string_of("connections[i].addresses[j].metadata.source.id").unwrap_or_default();
        let _ = json.string_of("connections[i].addresses[j].formattedValue").unwrap_or_default();
        let _ = json.string_of("connections[i].addresses[j].streetAddress").unwrap_or_default();
        let _ = json.string_of("connections[i].addresses[j].city").unwrap_or_default();
        let _ = json.string_of("connections[i].addresses[j].region").unwrap_or_default();
        let _ = json.string_of("connections[i].addresses[j].postalCode").unwrap_or_default();
        let _ = json.string_of("connections[i].addresses[j].country").unwrap_or_default();
        let _ = json.string_of("connections[i].addresses[j].countryCode").unwrap_or_default();
        j = j + 1;
    }

    i = i + 1;
}