Sample code for 30+ languages & platforms
Rust

Google People API - Create Contact

See more Google People Examples

Create a new contact.

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

// Implements the following CURL command:

// curl -H "Content-Type: application/json" -X POST
//  -d '{
//  "names": [{ "givenName": "John", "familyName": "Doe" }]
//  }'
//  https://people.googleapis.com/v1/people:createContact

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

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

// The following JSON is sent in the request body.

// {
//   "names": [
//     {
//       "givenName": "John",
//       "familyName": "Doe"
//     }
//   ]
// }

let json = chilkat::JsonObject::new();
let _ = json.update_string("names[0].givenName", "John");
let _ = json.update_string("names[0].familyName", "Doe");

let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://people.googleapis.com/v1/people:createContact", &json, "application/json", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);

println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());

let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
    println!("Response Header:");
    println!("{}", resp.header());
    println!("Failed.");
    return;
}

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "resourceName": "people/c172365763025317520",
//   "etag": "%EigBAj0DBAUGBwgJPgoLPwwNDg8QQBESExQVFhc1GTQ3HyEiIyQlJicuGgQBAgUHIgxleVowR3U1SFZhcz0=",
//   "metadata": {
//     "sources": [
//       {
//         "type": "CONTACT",
//         "id": "2645dbf8c902e90",
//         "etag": "#eyZ0Gu5HVas=",
//         "updateTime": "2020-08-03T22:46:03.410Z"
//       }
//     ],
//     "objectType": "PERSON"
//   },
//   "names": [
//     {
//       "metadata": {
//         "primary": true,
//         "source": {
//           "type": "CONTACT",
//           "id": "2645dbf8c902e90"
//         }
//       },
//       "displayName": "John Doe",
//       "familyName": "Doe",
//       "givenName": "John",
//       "displayNameLastFirst": "Doe, John",
//       "unstructuredName": "John Doe"
//     }
//   ],
//   "photos": [
//     {
//       "metadata": {
//         "primary": true,
//         "source": {
//           "type": "CONTACT",
//           "id": "2645dbf8c902e90"
//         }
//       },
//       "url": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/V8BNOaftJmYG2hHugNzYued7G9QFdbZOACLcDEAEiGQoBShD___________8BGNuwiP7______wE/s100/photo.jpg",
//       "default": true
//     }
//   ],
//   "memberships": [
//     {
//       "metadata": {
//         "source": {
//           "type": "CONTACT",
//           "id": "2645dbf8c902e90"
//         }
//       },
//       "contactGroupMembership": {
//         "contactGroupId": "myContacts",
//         "contactGroupResourceName": "contactGroups/myContacts"
//       }
//     }
//   ]
// }

// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

let resource_name = j_resp.string_of("resourceName").unwrap_or_default();
let mut etag = j_resp.string_of("etag").unwrap_or_default();
let metadata_object_type = j_resp.string_of("metadata.objectType").unwrap_or_default();
let mut i = 0;
let mut count_i = j_resp.size_of_array("metadata.sources");
while i < count_i {
    j_resp.set_i(i);
    let _ = j_resp.string_of("metadata.sources[i].type").unwrap_or_default();
    let _ = j_resp.string_of("metadata.sources[i].id").unwrap_or_default();
    etag = j_resp.string_of("metadata.sources[i].etag").unwrap_or_default();
    let _ = j_resp.string_of("metadata.sources[i].updateTime").unwrap_or_default();
    i = i + 1;
}

i = 0;
count_i = j_resp.size_of_array("names");
while i < count_i {
    j_resp.set_i(i);
    let _ = j_resp.bool_of("names[i].metadata.primary");
    let _ = j_resp.string_of("names[i].metadata.source.type").unwrap_or_default();
    let _ = j_resp.string_of("names[i].metadata.source.id").unwrap_or_default();
    let _ = j_resp.string_of("names[i].displayName").unwrap_or_default();
    let _ = j_resp.string_of("names[i].familyName").unwrap_or_default();
    let _ = j_resp.string_of("names[i].givenName").unwrap_or_default();
    let _ = j_resp.string_of("names[i].displayNameLastFirst").unwrap_or_default();
    let _ = j_resp.string_of("names[i].unstructuredName").unwrap_or_default();
    i = i + 1;
}

i = 0;
count_i = j_resp.size_of_array("photos");
while i < count_i {
    j_resp.set_i(i);
    let _ = j_resp.bool_of("photos[i].metadata.primary");
    let _ = j_resp.string_of("photos[i].metadata.source.type").unwrap_or_default();
    let _ = j_resp.string_of("photos[i].metadata.source.id").unwrap_or_default();
    let _ = j_resp.string_of("photos[i].url").unwrap_or_default();
    let _ = j_resp.bool_of("photos[i].default");
    i = i + 1;
}

i = 0;
count_i = j_resp.size_of_array("memberships");
while i < count_i {
    j_resp.set_i(i);
    let _ = j_resp.string_of("memberships[i].metadata.source.type").unwrap_or_default();
    let _ = j_resp.string_of("memberships[i].metadata.source.id").unwrap_or_default();
    let _ = j_resp.string_of("memberships[i].contactGroupMembership.contactGroupId").unwrap_or_default();
    let _ = j_resp.string_of("memberships[i].contactGroupMembership.contactGroupResourceName").unwrap_or_default();
    i = i + 1;
}