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

Constant Contact V3: Refresh a PKCE access token

See more Constant Contact Examples

Run desktop authorization first. Refresh near the saved expiry or after a 401. Reuse the original client ID and redirect URI and save any rotated refresh token with the new access token. Serialize refreshes for the same account so two processes cannot overwrite the newest grant. If the grant is invalid, authorize again.

Constant Contact developer guide

Chilkat Objective-C Downloads

Objective-C
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpRequest.h>
#import <CkoHttpResponse.h>
#import <CkoDateTime.h>

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoSecrets *vault = [[CkoSecrets alloc] init];
vault.Location = @"local_manager";
vault.AutoSplit = YES;
CkoJsonObject *entry = [[CkoJsonObject alloc] init];
[entry UpdateString: @"appName" value: @"ConstantContactV3"];
[entry UpdateString: @"service" value: @"OAuth2"];
[entry UpdateString: @"username" value: @"my-account"];
CkoJsonObject *tokens = [[CkoJsonObject alloc] init];
BOOL loaded = [vault GetSecretJson: entry jsonOut: tokens];
if (loaded != YES) {
    NSLog(@"%@",@"Run the desktop authorization example using this account and store.");
    return;
}

if (([[tokens JsonTypeOf: @"refresh_token"] intValue] != 1) || ([[tokens JsonTypeOf: @"client_id"] intValue] != 1) || ([[tokens JsonTypeOf: @"redirect_uri"] intValue] != 1)) {
    NSLog(@"%@",@"The saved PKCE grant is incomplete. Authorize again.");
    return;
}

CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
CkoHttpRequest *form = [[CkoHttpRequest alloc] init];
form.HttpVerb = @"POST";
form.ContentType = @"application/x-www-form-urlencoded";
[form AddHeader: @"Accept" value: @"application/json"];
[form AddParam: @"grant_type" value: @"refresh_token"];
[form AddParam: @"client_id" value: [tokens StringOf: @"client_id"]];
[form AddParam: @"redirect_uri" value: [tokens StringOf: @"redirect_uri"]];
[form AddParam: @"refresh_token" value: [tokens StringOf: @"refresh_token"]];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
BOOL sent = [http HttpReq: @"https://authz.constantcontact.com/oauth2/default/v1/token" request: form response: resp];
CkoJsonObject *reply = [[CkoJsonObject alloc] init];
BOOL parsed = [reply Load: resp.BodyStr];
if ((sent != YES) || ([resp.StatusCode intValue] != 200)) {
    NSLog(@"%@%d",@"Token refresh failed. HTTP status ",[resp.StatusCode intValue]);
    if ([reply StringOfEquals: @"error" value: @"invalid_grant" caseSensitive: YES] == YES) {
        NSLog(@"%@",@"The refresh grant is no longer valid. Authorize again.");
    }

    return;
}

if (([[reply JsonTypeOf: @"access_token"] intValue] != 1) || ([[reply IntOf: @"expires_in"] intValue] <= 0)) {
    NSLog(@"%@",@"Token response is missing an access token or lifetime.");
    return;
}

if ([reply StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES) {
    NSLog(@"%@",@"The returned access token is empty.");
    return;
}

if ([[reply JsonTypeOf: @"refresh_token"] intValue] == 1) {
    if ([reply StringOfEquals: @"refresh_token" value: @"" caseSensitive: YES] != YES) {
        [tokens UpdateString: @"refresh_token" value: [reply StringOf: @"refresh_token"]];
    }

}

CkoDateTime *now = [[CkoDateTime alloc] init];
[now SetFromCurrentSystemTime];
[tokens UpdateString: @"obtained_at_utc" value: [now GetAsTimestamp: NO]];
[tokens UpdateString: @"access_token" value: [reply StringOf: @"access_token"]];
[tokens UpdateInt: @"expires_in" value: [reply IntOf: @"expires_in"]];
BOOL saved = [vault UpdateSecretJson: entry secret: tokens];
if (saved != YES) {
    NSLog(@"%@",@"Could not save the current token record. Check the credential store.");
    return;
}

NSLog(@"%@%d",@"Authorization saved. Access-token lifetime in seconds: ",[[tokens IntOf: @"expires_in"] intValue]);