Unicode C Requires Chilkat v11.0.0+
Unicode C
PayPal: Get a sandbox access token
See more PayPal Examples
Exchange a sandbox REST app client ID and secret for a bearer token on a merchant-controlled backoffice workstation. PayPal supplies its lifetime in seconds. Store the token in a protected credential store; the companion expiry example reads only timing metadata.
Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkDateTimeW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkDateTimeW issued;
HCkHttpResponseW resp;
HCkJsonObjectW errorInfo;
HCkJsonObjectW result;
BOOL parsed;
const wchar_t *accessToken;
int lifetime;
HCkJsonObjectW timing;
BOOL savedTiming;
success = FALSE;
// See Unlock Chilkat before making requests.
http = CkHttpW_Create();
// Load these values from the credential store on the merchant backoffice workstation.
// Never embed the merchant secret in an app distributed to customers.
CkHttpW_putLogin(http,L"SANDBOX_CLIENT_ID");
CkHttpW_putPassword(http,L"SANDBOX_CLIENT_SECRET");
CkHttpW_putBasicAuth(http,TRUE);
issued = CkDateTimeW_Create();
CkDateTimeW_SetFromCurrentSystemTime(issued);
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpStr(http,L"POST",L"https://api-m.sandbox.paypal.com/v1/oauth2/token",L"grant_type=client_credentials",L"utf-8",L"application/x-www-form-urlencoded",resp);
if (success != TRUE) {
wprintf(L"No HTTP response: %s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkDateTimeW_Dispose(issued);
CkHttpResponseW_Dispose(resp);
return;
}
if ((CkHttpResponseW_getStatusCode(resp) < 200) || (CkHttpResponseW_getStatusCode(resp) >= 300)) {
errorInfo = CkJsonObjectW_Create();
CkJsonObjectW_Load(errorInfo,CkHttpResponseW_bodyStr(resp));
wprintf(L"HTTP %d %s %s\n",CkHttpResponseW_getStatusCode(resp),CkJsonObjectW_stringOf(errorInfo,L"error"),CkJsonObjectW_stringOf(errorInfo,L"error_description"));
wprintf(L"PayPal debug ID: %s\n",CkJsonObjectW_stringOf(errorInfo,L"debug_id"));
CkHttpW_Dispose(http);
CkDateTimeW_Dispose(issued);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(errorInfo);
return;
}
result = CkJsonObjectW_Create();
parsed = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
if (parsed != TRUE) {
wprintf(L"Expected a JSON response; HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
CkHttpW_Dispose(http);
CkDateTimeW_Dispose(issued);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(errorInfo);
CkJsonObjectW_Dispose(result);
return;
}
accessToken = CkJsonObjectW_stringOf(result,L"access_token");
lifetime = CkJsonObjectW_IntOf(result,L"expires_in");
if ((CkJsonObjectW_HasMember(result,L"access_token") != TRUE) || (lifetime <= 0)) {
wprintf(L"Missing token or lifetime.\n");
CkHttpW_Dispose(http);
CkDateTimeW_Dispose(issued);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(errorInfo);
CkJsonObjectW_Dispose(result);
return;
}
// Supply accessToken to Http.AuthToken. Never log the bearer token or client secret.
// Save only timing metadata here, relative to the start of the token request.
timing = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(timing,L"issued_at",CkDateTimeW_getAsTimestamp(issued,FALSE));
CkJsonObjectW_UpdateInt(timing,L"expires_in",lifetime);
savedTiming = CkJsonObjectW_WriteFile(timing,L"paypal-token-timing.json");
if (savedTiming != TRUE) {
wprintf(L"Could not save token timing.\n");
CkHttpW_Dispose(http);
CkDateTimeW_Dispose(issued);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(errorInfo);
CkJsonObjectW_Dispose(result);
CkJsonObjectW_Dispose(timing);
return;
}
wprintf(L"Token received; lifetime in seconds: %d\n",lifetime);
CkHttpW_Dispose(http);
CkDateTimeW_Dispose(issued);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(errorInfo);
CkJsonObjectW_Dispose(result);
CkJsonObjectW_Dispose(timing);
}