Constant Contact V3: Authorize a desktop app with PKCE
See more Constant Contact Examples
Register a V3 app for the public-client PKCE flow and register http://localhost:3017/ exactly, including the trailing slash. Chilkat opens the system browser, receives the redirect on the same computer, and exchanges the code using the PKCE verifier. Request contact_data for contacts and lists, campaign_data for email campaigns and reporting, and offline_access for refresh. If the app already has a contacts-only grant, repeat this browser authorization to grant campaign access. Refreshing the old token cannot add a scope. Use a client ID from the PKCE app registration.
The examples share a Chilkat Secrets record. Windows Credential Manager and Apple Keychain persist the local record. On systems where the local manager uses process memory, configure a persistent Secrets backend for all examples. Give each authorized account its own record name. If migrating from the old token-file example, authorize once to populate the store.
Constant Contact developer guide
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
DECLARE @oauth int
EXEC @hr = sp_OACreate 'Chilkat.OAuth2', @oauth OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @oauth, 'AuthorizationEndpoint', 'https://authz.constantcontact.com/oauth2/default/v1/authorize'
EXEC sp_OASetProperty @oauth, 'TokenEndpoint', 'https://authz.constantcontact.com/oauth2/default/v1/token'
EXEC sp_OASetProperty @oauth, 'ClientId', 'PKCE_CLIENT_ID'
EXEC sp_OASetProperty @oauth, 'LocalHost', 'localhost'
EXEC sp_OASetProperty @oauth, 'ListenPort', 3017
-- Run authorization again if the account previously granted only contact access.
EXEC sp_OASetProperty @oauth, 'Scope', 'contact_data campaign_data offline_access'
EXEC sp_OASetProperty @oauth, 'CodeChallenge', 1
EXEC sp_OASetProperty @oauth, 'CodeChallengeMethod', 'S256'
EXEC sp_OASetProperty @oauth, 'UseBasicAuth', 0
DECLARE @url nvarchar(4000)
EXEC sp_OAMethod @oauth, 'StartAuth', @url OUT
EXEC sp_OAGetProperty @oauth, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Could not start the local listener. Check the registered port.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
DECLARE @opened int
EXEC sp_OAMethod @oauth, 'LaunchBrowser', @opened OUT, @url
IF @opened <> 1
BEGIN
DECLARE @success int
EXEC sp_OAMethod @oauth, 'Cancel', @success OUT
PRINT 'Could not open the system browser.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
DECLARE @waited int
SELECT @waited = 0
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
WHILE ((@waited < 300000) and (@iTmp0 < 3))
BEGIN
EXEC sp_OAMethod @oauth, 'SleepMs', NULL, 100
SELECT @waited = @waited + 100
END
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
IF @iTmp0 < 3
BEGIN
EXEC sp_OAMethod @oauth, 'Cancel', @success OUT
PRINT 'Authorization timed out.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
IF @iTmp0 = 4
BEGIN
PRINT 'Authorization was declined.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
IF @iTmp0 <> 3
BEGIN
PRINT 'Authorization failed. Check the PKCE app registration and exact redirect URI.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
DECLARE @reply int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @reply OUT
DECLARE @parsed int
EXEC sp_OAGetProperty @oauth, 'AccessTokenResponse', @sTmp0 OUT
EXEC sp_OAMethod @reply, 'Load', @parsed OUT, @sTmp0
EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
EXEC sp_OAMethod @reply, 'IntOf', @iTmp1 OUT, 'expires_in'
IF (@iTmp0 <> 1) or (@iTmp1 <= 0)
BEGIN
PRINT 'Token response is missing an access token or lifetime.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @reply
RETURN
END
EXEC sp_OAMethod @reply, 'StringOfEquals', @iTmp0 OUT, 'access_token', '', 1
IF @iTmp0 = 1
BEGIN
PRINT 'The returned access token is empty.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @reply
RETURN
END
EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @reply, 'StringOfEquals', @iTmp1 OUT, 'refresh_token', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Missing refresh token. Check the offline_access scope and authorize again.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @reply
RETURN
END
DECLARE @vault int
EXEC @hr = sp_OACreate 'Chilkat.Secrets', @vault OUT
EXEC sp_OASetProperty @vault, 'Location', 'local_manager'
EXEC sp_OASetProperty @vault, 'AutoSplit', 1
DECLARE @entry int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @entry OUT
EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'appName', 'ConstantContactV3'
EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'service', 'OAuth2'
EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'username', 'my-account'
DECLARE @tokens int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @tokens OUT
EXEC sp_OAGetProperty @oauth, 'ClientId', @sTmp0 OUT
EXEC sp_OAMethod @tokens, 'UpdateString', @success OUT, 'client_id', @sTmp0
EXEC sp_OAMethod @tokens, 'UpdateString', @success OUT, 'redirect_uri', 'http://localhost:3017/'
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @tokens, 'UpdateString', @success OUT, 'refresh_token', @sTmp0
DECLARE @now int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @now OUT
EXEC sp_OAMethod @now, 'SetFromCurrentSystemTime', @success OUT
EXEC sp_OAMethod @now, 'GetAsTimestamp', @sTmp0 OUT, 0
EXEC sp_OAMethod @tokens, 'UpdateString', @success OUT, 'obtained_at_utc', @sTmp0
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OAMethod @tokens, 'UpdateString', @success OUT, 'access_token', @sTmp0
EXEC sp_OAMethod @reply, 'IntOf', @iTmp0 OUT, 'expires_in'
EXEC sp_OAMethod @tokens, 'UpdateInt', @success OUT, 'expires_in', @iTmp0
DECLARE @saved int
EXEC sp_OAMethod @vault, 'UpdateSecretJson', @saved OUT, @entry, @tokens
IF @saved <> 1
BEGIN
PRINT 'Could not save the current token record. Check the credential store.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @vault
EXEC @hr = sp_OADestroy @entry
EXEC @hr = sp_OADestroy @tokens
EXEC @hr = sp_OADestroy @now
RETURN
END
EXEC sp_OAMethod @tokens, 'IntOf', @iTmp0 OUT, 'expires_in'
PRINT 'Authorization saved. Access-token lifetime in seconds: ' + @iTmp0
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @vault
EXEC @hr = sp_OADestroy @entry
EXEC @hr = sp_OADestroy @tokens
EXEC @hr = sp_OADestroy @now
END
GO