Unicode C
Unicode C
Download Google Contact Photo
See more Google APIs Examples
Demonstrates how to download Google Contact's photo.Chilkat Unicode C Downloads
#include <C_CkJsonObjectW.h>
#include <C_CkAuthGoogleW.h>
#include <C_CkRestW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkBinDataW.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObjectW jsonToken;
HCkAuthGoogleW gAuth;
HCkRestW rest;
BOOL bAutoReconnect;
HCkStringBuilderW sbPath;
const wchar_t *contactId;
int numReplacements;
HCkBinDataW imageData;
HCkStringBuilderW sbResponseBody;
HCkStringBuilderW 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 = CkJsonObjectW_Create();
success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/googleContacts.json");
if (success != TRUE) {
wprintf(L"Failed to load googleContacts.json\n");
CkJsonObjectW_Dispose(jsonToken);
return;
}
gAuth = CkAuthGoogleW_Create();
CkAuthGoogleW_putAccessToken(gAuth,CkJsonObjectW_stringOf(jsonToken,L"access_token"));
rest = CkRestW_Create();
// Connect using TLS.
bAutoReconnect = TRUE;
success = CkRestW_Connect(rest,L"www.google.com",443,TRUE,bAutoReconnect);
// Provide the authentication credentials (i.e. the access token)
CkRestW_SetAuthGoogle(rest,gAuth);
// ----------------------------------------------
// OK, the REST connection setup is completed..
// ----------------------------------------------
// To get the photo, send the following:
// GET /m8/feeds/photos/media/default/contactId
CkRestW_AddHeader(rest,L"GData-Version",L"3.0");
sbPath = CkStringBuilderW_Create();
// Get the photo for the contact having contactId = "1ea2e4fe0ef24e09"
contactId = L"1ea2e4fe0ef24e09";
CkStringBuilderW_SetString(sbPath,L"/m8/feeds/photos/media/default/{contactId}");
numReplacements = CkStringBuilderW_Replace(sbPath,L"{contactId}",contactId);
imageData = CkBinDataW_Create();
success = CkRestW_FullRequestNoBodyBd(rest,L"GET",CkStringBuilderW_getAsString(sbPath),imageData);
if (success != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkJsonObjectW_Dispose(jsonToken);
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkBinDataW_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 (CkRestW_getResponseStatusCode(rest) == 404) {
wprintf(L"This contact has no photo.\n");
CkJsonObjectW_Dispose(jsonToken);
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkBinDataW_Dispose(imageData);
return;
}
// A successful response will have a status code equal to 200.
if (CkRestW_getResponseStatusCode(rest) != 200) {
// If the response was not successful, then the response body
// does not contain image data. Instead it contains XML.
sbResponseBody = CkStringBuilderW_Create();
CkStringBuilderW_AppendBd(sbResponseBody,imageData,L"utf-8",0,0);
wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest));
wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest));
wprintf(L"response header: %s\n",CkRestW_responseHeader(rest));
wprintf(L"response body: %s\n",CkStringBuilderW_getAsString(sbResponseBody));
wprintf(L"request startline: %s\n",CkRestW_lastRequestStartLine(rest));
wprintf(L"request header: %s\n",CkRestW_lastRequestHeader(rest));
CkJsonObjectW_Dispose(jsonToken);
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkBinDataW_Dispose(imageData);
CkStringBuilderW_Dispose(sbResponseBody);
return;
}
// Examine the content-type in the response header so we know what file
// extension to use (.jpg, .png, etc.)
sbContentType = CkStringBuilderW_Create();
CkStringBuilderW_Append(sbContentType,CkRestW_responseHdrByName(rest,L"Content-Type"));
if (CkStringBuilderW_ContentsEqual(sbContentType,L"image/jpeg",FALSE) == TRUE) {
CkBinDataW_WriteFile(imageData,L"qa_output/contact_photo.jpg");
}
if (CkStringBuilderW_ContentsEqual(sbContentType,L"image/png",FALSE) == TRUE) {
CkBinDataW_WriteFile(imageData,L"qa_output/contact_photo.png");
}
wprintf(L"Content-Type: %s\n",CkStringBuilderW_getAsString(sbContentType));
wprintf(L"Success.\n");
CkJsonObjectW_Dispose(jsonToken);
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkBinDataW_Dispose(imageData);
CkStringBuilderW_Dispose(sbResponseBody);
CkStringBuilderW_Dispose(sbContentType);
}