Sample code for 30+ languages & platforms
C

Download Google Contact Photo

See more Google APIs Examples

Demonstrates how to download Google Contact's photo.

Chilkat C Downloads

C
#include <C_CkJsonObject.h>
#include <C_CkAuthGoogle.h>
#include <C_CkRest.h>
#include <C_CkStringBuilder.h>
#include <C_CkBinData.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObject jsonToken;
    HCkAuthGoogle gAuth;
    HCkRest rest;
    BOOL bAutoReconnect;
    HCkStringBuilder sbPath;
    const char *contactId;
    int numReplacements;
    HCkBinData imageData;
    HCkStringBuilder sbResponseBody;
    HCkStringBuilder sbContentType;

    success = FALSE;

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

    // --------------------------------------------------------------------------------------------------------
    // Note: The code for setting up the Chilkat REST object and making the initial connection can be done once.
    // Once connected, the REST object may be re-used for many REST API calls.
    // (It's a good idea to put the connection setup code in a separate function/subroutine.)
    // --------------------------------------------------------------------------------------------------------

    // It is assumed we previously obtained an OAuth2 access token.
    // This example loads the JSON access token file 
    // saved by this example: Get Google Contacts OAuth2 Access Token

    jsonToken = CkJsonObject_Create();
    success = CkJsonObject_LoadFile(jsonToken,"qa_data/tokens/googleContacts.json");
    if (success != TRUE) {
        printf("Failed to load googleContacts.json\n");
        CkJsonObject_Dispose(jsonToken);
        return;
    }

    gAuth = CkAuthGoogle_Create();
    CkAuthGoogle_putAccessToken(gAuth,CkJsonObject_stringOf(jsonToken,"access_token"));

    rest = CkRest_Create();

    // Connect using TLS.
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"www.google.com",443,TRUE,bAutoReconnect);

    // Provide the authentication credentials (i.e. the access token)
    CkRest_SetAuthGoogle(rest,gAuth);

    // ----------------------------------------------
    // OK, the REST connection setup is completed..
    // ----------------------------------------------

    // To get the photo, send the following:

    // 	GET /m8/feeds/photos/media/default/contactId

    CkRest_AddHeader(rest,"GData-Version","3.0");

    sbPath = CkStringBuilder_Create();
    // Get the photo for the contact having contactId = "1ea2e4fe0ef24e09"
    contactId = "1ea2e4fe0ef24e09";
    CkStringBuilder_SetString(sbPath,"/m8/feeds/photos/media/default/{contactId}");
    numReplacements = CkStringBuilder_Replace(sbPath,"{contactId}",contactId);

    imageData = CkBinData_Create();
    success = CkRest_FullRequestNoBodyBd(rest,"GET",CkStringBuilder_getAsString(sbPath),imageData);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkJsonObject_Dispose(jsonToken);
        CkAuthGoogle_Dispose(gAuth);
        CkRest_Dispose(rest);
        CkStringBuilder_Dispose(sbPath);
        CkBinData_Dispose(imageData);
        return;
    }

    // A 404 response indicates the contact has no photo.
    // (We could've first fetched the contact information, parsed out the 
    // photo etag, and then if no photo etag existed, we'd know the contact has no
    // photo.  Or... we can just try to download the photo and if a 404 is received,
    // we know there's no photo.  Much simpler.)
    if (CkRest_getResponseStatusCode(rest) == 404) {
        printf("This contact has no photo.\n");
        CkJsonObject_Dispose(jsonToken);
        CkAuthGoogle_Dispose(gAuth);
        CkRest_Dispose(rest);
        CkStringBuilder_Dispose(sbPath);
        CkBinData_Dispose(imageData);
        return;
    }

    // A successful response will have a status code equal to 200.
    if (CkRest_getResponseStatusCode(rest) != 200) {
        // If the response was not successful, then the response body
        // does not contain image data.  Instead it contains XML.
        sbResponseBody = CkStringBuilder_Create();
        CkStringBuilder_AppendBd(sbResponseBody,imageData,"utf-8",0,0);

        printf("response status code = %d\n",CkRest_getResponseStatusCode(rest));
        printf("response status text = %s\n",CkRest_responseStatusText(rest));
        printf("response header: %s\n",CkRest_responseHeader(rest));
        printf("response body: %s\n",CkStringBuilder_getAsString(sbResponseBody));
        printf("request startline: %s\n",CkRest_lastRequestStartLine(rest));
        printf("request header: %s\n",CkRest_lastRequestHeader(rest));
        CkJsonObject_Dispose(jsonToken);
        CkAuthGoogle_Dispose(gAuth);
        CkRest_Dispose(rest);
        CkStringBuilder_Dispose(sbPath);
        CkBinData_Dispose(imageData);
        CkStringBuilder_Dispose(sbResponseBody);
        return;
    }

    // Examine the content-type in the response header so we know what file
    // extension to use (.jpg, .png, etc.)
    sbContentType = CkStringBuilder_Create();
    CkStringBuilder_Append(sbContentType,CkRest_responseHdrByName(rest,"Content-Type"));

    if (CkStringBuilder_ContentsEqual(sbContentType,"image/jpeg",FALSE) == TRUE) {
        CkBinData_WriteFile(imageData,"qa_output/contact_photo.jpg");
    }

    if (CkStringBuilder_ContentsEqual(sbContentType,"image/png",FALSE) == TRUE) {
        CkBinData_WriteFile(imageData,"qa_output/contact_photo.png");
    }

    printf("Content-Type: %s\n",CkStringBuilder_getAsString(sbContentType));
    printf("Success.\n");


    CkJsonObject_Dispose(jsonToken);
    CkAuthGoogle_Dispose(gAuth);
    CkRest_Dispose(rest);
    CkStringBuilder_Dispose(sbPath);
    CkBinData_Dispose(imageData);
    CkStringBuilder_Dispose(sbResponseBody);
    CkStringBuilder_Dispose(sbContentType);

    }