Sample code for 30+ languages & platforms
C Requires Chilkat v11.0.0+

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.

PayPal documentation

Chilkat C Downloads

C
#include <C_CkHttp.h>
#include <C_CkDateTime.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkDateTime issued;
    HCkHttpResponse resp;
    HCkJsonObject errorInfo;
    HCkJsonObject result;
    BOOL parsed;
    const char *accessToken;
    int lifetime;
    HCkJsonObject timing;
    BOOL savedTiming;

    success = FALSE;

    //  See Unlock Chilkat before making requests.
    http = CkHttp_Create();
    //  Load these values from the credential store on the merchant backoffice workstation.
    //  Never embed the merchant secret in an app distributed to customers.
    CkHttp_putLogin(http,"SANDBOX_CLIENT_ID");
    CkHttp_putPassword(http,"SANDBOX_CLIENT_SECRET");
    CkHttp_putBasicAuth(http,TRUE);
    issued = CkDateTime_Create();
    CkDateTime_SetFromCurrentSystemTime(issued);
    resp = CkHttpResponse_Create();
    success = CkHttp_HttpStr(http,"POST","https://api-m.sandbox.paypal.com/v1/oauth2/token","grant_type=client_credentials","utf-8","application/x-www-form-urlencoded",resp);
    if (success != TRUE) {
        printf("No HTTP response: %s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkDateTime_Dispose(issued);
        CkHttpResponse_Dispose(resp);
        return;
    }

    if ((CkHttpResponse_getStatusCode(resp) < 200) || (CkHttpResponse_getStatusCode(resp) >= 300)) {
        errorInfo = CkJsonObject_Create();
        CkJsonObject_Load(errorInfo,CkHttpResponse_bodyStr(resp));
        printf("HTTP %d %s %s\n",CkHttpResponse_getStatusCode(resp),CkJsonObject_stringOf(errorInfo,"error"),CkJsonObject_stringOf(errorInfo,"error_description"));
        printf("PayPal debug ID: %s\n",CkJsonObject_stringOf(errorInfo,"debug_id"));
        CkHttp_Dispose(http);
        CkDateTime_Dispose(issued);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        return;
    }

    result = CkJsonObject_Create();
    parsed = CkJsonObject_Load(result,CkHttpResponse_bodyStr(resp));
    if (parsed != TRUE) {
        printf("Expected a JSON response; HTTP %d\n",CkHttpResponse_getStatusCode(resp));
        CkHttp_Dispose(http);
        CkDateTime_Dispose(issued);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        CkJsonObject_Dispose(result);
        return;
    }

    accessToken = CkJsonObject_stringOf(result,"access_token");
    lifetime = CkJsonObject_IntOf(result,"expires_in");
    if ((CkJsonObject_HasMember(result,"access_token") != TRUE) || (lifetime <= 0)) {
        printf("Missing token or lifetime.\n");
        CkHttp_Dispose(http);
        CkDateTime_Dispose(issued);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        CkJsonObject_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 = CkJsonObject_Create();
    CkJsonObject_UpdateString(timing,"issued_at",CkDateTime_getAsTimestamp(issued,FALSE));
    CkJsonObject_UpdateInt(timing,"expires_in",lifetime);
    savedTiming = CkJsonObject_WriteFile(timing,"paypal-token-timing.json");
    if (savedTiming != TRUE) {
        printf("Could not save token timing.\n");
        CkHttp_Dispose(http);
        CkDateTime_Dispose(issued);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        CkJsonObject_Dispose(result);
        CkJsonObject_Dispose(timing);
        return;
    }

    printf("Token received; lifetime in seconds: %d\n",lifetime);


    CkHttp_Dispose(http);
    CkDateTime_Dispose(issued);
    CkHttpResponse_Dispose(resp);
    CkJsonObject_Dispose(errorInfo);
    CkJsonObject_Dispose(result);
    CkJsonObject_Dispose(timing);

    }