Sample code for 30+ languages & platforms
Java

Google People API - Create Contact

See more Google People Examples

Create a new contact.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // 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

        JsonObject jsonToken = new JsonObject();
        try {
            jsonToken.loadFile("qa_data/tokens/googlePeople.json");
        } catch (ChilkatException e) {
            System.out.println("Failed to load googleContacts.json");
            return;
        }

        Http http = new Http();

        http.setAuthToken(jsonToken.stringOf("access_token"));

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

        JsonObject json = new JsonObject();
        json.updateString("names[0].givenName", "John");
        json.updateString("names[0].familyName", "Doe");

        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", "https://people.googleapis.com/v1/people:createContact", json, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);
        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.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

        String v_type = "";
        String id = "";
        String updateTime = "";
        boolean metadataPrimary = false;
        String metadataSourceType = "";
        String metadataSourceId = "";
        String displayName = "";
        String familyName = "";
        String givenName = "";
        String displayNameLastFirst = "";
        String unstructuredName = "";
        String url = "";
        boolean default_ = false;
        String contactGroupMembershipContactGroupId = "";
        String contactGroupMembershipContactGroupResourceName = "";

        String resourceName = jResp.stringOf("resourceName");
        String etag = jResp.stringOf("etag");
        String metadataObjectType = jResp.stringOf("metadata.objectType");
        int i = 0;
        int count_i = jResp.sizeOfArray("metadata.sources");
        while (i < count_i) {
            jResp.setI(i);
            v_type = jResp.stringOf("metadata.sources[i].type");
            id = jResp.stringOf("metadata.sources[i].id");
            etag = jResp.stringOf("metadata.sources[i].etag");
            updateTime = jResp.stringOf("metadata.sources[i].updateTime");
            i = i + 1;
        }

        i = 0;
        count_i = jResp.sizeOfArray("names");
        while (i < count_i) {
            jResp.setI(i);
            metadataPrimary = jResp.boolOf("names[i].metadata.primary");
            metadataSourceType = jResp.stringOf("names[i].metadata.source.type");
            metadataSourceId = jResp.stringOf("names[i].metadata.source.id");
            displayName = jResp.stringOf("names[i].displayName");
            familyName = jResp.stringOf("names[i].familyName");
            givenName = jResp.stringOf("names[i].givenName");
            displayNameLastFirst = jResp.stringOf("names[i].displayNameLastFirst");
            unstructuredName = jResp.stringOf("names[i].unstructuredName");
            i = i + 1;
        }

        i = 0;
        count_i = jResp.sizeOfArray("photos");
        while (i < count_i) {
            jResp.setI(i);
            metadataPrimary = jResp.boolOf("photos[i].metadata.primary");
            metadataSourceType = jResp.stringOf("photos[i].metadata.source.type");
            metadataSourceId = jResp.stringOf("photos[i].metadata.source.id");
            url = jResp.stringOf("photos[i].url");
            default_ = jResp.boolOf("photos[i].default");
            i = i + 1;
        }

        i = 0;
        count_i = jResp.sizeOfArray("memberships");
        while (i < count_i) {
            jResp.setI(i);
            metadataSourceType = jResp.stringOf("memberships[i].metadata.source.type");
            metadataSourceId = jResp.stringOf("memberships[i].metadata.source.id");
            contactGroupMembershipContactGroupId = jResp.stringOf("memberships[i].contactGroupMembership.contactGroupId");
            contactGroupMembershipContactGroupResourceName = jResp.stringOf("memberships[i].contactGroupMembership.contactGroupResourceName");
            i = i + 1;
        }
    }
}