Skip to content

Instantly share code, notes, and snippets.

@cmaggiulli
Created December 2, 2023 19:24
Show Gist options
  • Save cmaggiulli/1a21e838dd1bfaf6ed9c9c605bec0793 to your computer and use it in GitHub Desktop.
Save cmaggiulli/1a21e838dd1bfaf6ed9c9c605bec0793 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script will dump all smartCampaign data from Marketo into a
# semantically valid JSON file for consumption by downstream processes
# Author : Chris Maggiulli ( [email protected] )
echo "Script Start Time $(date)"
# Set script parameters
grant_type="client_credentials"
client_id="$2"
client_secret="$3"
host_name="$1"
# Function to obtain the bearer token
get_bearer_token() {
# Make the curl request to obtain the bearer token
token_response=$(curl -s --location "$host_name/identity/oauth/token?grant_type=$grant_type&client_id=$client_id&client_secret=$client_secret" \
--header 'Accept: application/json' \
--header 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8')
# Extract and return the bearer token from the response
echo "$token_response" | jq -r '.access_token'
}
# Set the initial offset
offset=0
# Name of the output file
output_file="campaign.json"
# Obtain the initial bearer token
BEARER_TOKEN=$(get_bearer_token)
# the output from each http request in the while loop is a json object
# and since each iteration would simply append a new line and another json object
# to the file it would result in invalid JSON. Therefore we need to
# nested all JSON objects in an anonymous top level array, and append a
# comma to each line to form a top-level anonymous array of objects
echo "[" > "$output_file"
while true; do
echo "Current iteration : $offset to $((offset + 200))"
# Make the GET request using curl with the bearer token
response=$(curl -s -H "Authorization: Bearer $BEARER_TOKEN" "$host_name/rest/asset/v1/smartCampaigns.json?maxReturn=200&offset=$offset")
# Check if the response contains the termination string
if [[ $response == *"warnings":["No assets found for the given search criteria."]* ]]; then
echo "Termination string found. Exiting loop."
break
fi
# Append the response to the output file. also append a comma to
# create a valid json file
echo "$response""," >> "$output_file"
# Increment the offset for the next request
offset=$((offset + 200))
# Obtain a new bearer token for the next request
BEARER_TOKEN=$(get_bearer_token)
# Optional: Add a delay to avoid rate limiting
sleep 1
done
# remove the last character ( which will be a comma )
# since its the last array item
sed -i '$ s/,$//' $output_file
# balance the anonymous array
echo "]" >> "$output_file"
echo "Script End Time $(date)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment