Skip to content

Instantly share code, notes, and snippets.

@camrobert
Created July 3, 2022 22:57
Show Gist options
  • Save camrobert/32d6b4d53704dee9f0245a74421cb5f9 to your computer and use it in GitHub Desktop.
Save camrobert/32d6b4d53704dee9f0245a74421cb5f9 to your computer and use it in GitHub Desktop.
AMPscript to Encrypt & Protect your API Credentials, followed by AMPscript to Decrypt and then use those values in an API call
%%[
/*SET Your 3 Keys*/
SET @sym = 'SymmetricKey ExternalKey'
SET @IV = 'IV ExternalKey'
SET @Salt = 'Salt ExternalKey'
/*Input Auth URI Details*/
SET @authurl = 'Authentication Base URI'
/*TEMP API Key Details - To Delete after generating encrypted values*/
SET @text_client_id = 'Plain Text Client ID'
SET @text_client_secret = 'Plain Text Client Secret'
/*TEMP Encrypt the API Key Values - delete after using*/
SET @enc_client_id = EncryptSymmetric(@text_client_id, 'AES', @sym, @null, @Salt, @null, @IV, @null)
SET @enc_client_secret = EncryptSymmetric(@text_client_secret, 'AES', @sym, @null, @Salt, @null, @IV, @null)
/*TEMP Output the values to copy - delete after using*/
OutputLine(Concat("<b>Copy these Encrypted Values into the DecryptSymmetric() functions:</b>","<br>"))
OutputLine(Concat("enc_client_id: ",@enc_client_id,"<br>"))
OutputLine(Concat("enc_client_secret: ",@enc_client_secret,"<br>"))
/*Replace the '@enc_' values with the Encoded text*/
SET @client_id = DecryptSymmetric(@enc_client_id, 'AES', @sym, @null, @Salt, @null, @IV, @null)
SET @client_secret = DecryptSymmetric(@enc_client_secret, 'AES', @sym, @null, @Salt, @null, @IV, @null)
/*Validate the API Keys work by making a ValidateEmail Call*/
SET @apiid = Concat('{"client_id": "',@client_id,'","client_secret": "',@client_secret,'", "grant_type": "client_credentials"}')
SET @httppost = HTTPPost2(Concat(@authurl,'v2/token'),"application/json",@apiid,false,@apistatusCode,@apiresponse)
SET @access_token = REGEXMATCH(@apistatusCode,'^(?:.*"access_token":")(.*?)(?:".*)$',1)
SET @rest_instance_url = REGEXMATCH(@apistatusCode,'^(?:.*"rest_instance_url":")(.*?)(?:".*)$',1)
SET @Email = "CameronRobert@youtube.com"
SET @payload = Concat('{"email": "',@Email,'","validators": [ "SyntaxValidator", "MXValidator", "ListDetectiveValidator" ]}')
SET @validateemail = HTTPPost2(Concat(@rest_instance_url,"address/v1/validateEmail"),"application/json",@payload,false,@output,@respheader,'Authorization', Concat('Bearer ',@access_token))
]%%
<br><br>
<b>validateEmail Result:</b><br>
Status: %%=v(@validateemail)=%%<br>
Output: %%=v(@output)=%%<br>
<br>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment