Skip to content

Instantly share code, notes, and snippets.

@royharmon4
Created October 16, 2019 16:24
Show Gist options
  • Select an option

  • Save royharmon4/bfeead341a28b657d1dab402b6e16809 to your computer and use it in GitHub Desktop.

Select an option

Save royharmon4/bfeead341a28b657d1dab402b6e16809 to your computer and use it in GitHub Desktop.
import os
import sys
import urllib
import hashlib
import hmac
import base64
import json
import random
import requests
headers = {"Accept":"application/json","Content-type":"application/json","User_Agent":"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5"}
# 1. Enter your BlueKai developer keys
bkuid = '[ID]' #Web Service User Key
bksecretkey = '[KEY]' #Web Service Private Key
bkuid_b = bytes(bkuid, 'utf-8')
bksecretkey_b = bytes(bksecretkey , 'utf-8')
# 2. Specify the service endpoint
# - For GET (List) requests, add the desired sort and filter options in the query string
# - For GET (Read), PUT or DELETE requests, append the item ID to the Url path
# * NOTE: For the Campaign, Order, and Pixel URL APIs, insert the item ID in the query string instead of the Url path
Url = 'http://services.bluekai.com/Services/WS/audiences/?partner_id=[PARTNER ID]'
# 3. For POST and PUT requests, uncomment the "data" variable and enter the JSON body
general = ''
move = '''
{"cat":164405},
{"cat":316130},
{"cat":840061},
{"cat":912831}
'''
health = '''
{"cat":270459}
'''
markets = ['''
{"cat":1672987},
{"cat":1673012}
''',
'''
{"cat":1673005},
{"cat":1672999}
''',
'''
{"cat":1673013},
{"cat":1672994}
''',
'''
{"cat":1673016},
{"cat":1672995}
''',
'''
{"cat":1673001},
{"cat":1672990}
''',
'''
{"cat":1672997},
{"cat":1672986}
''',
'''
{"cat":1673002},
{"cat":1672998}
''',
'''
{"cat":1673008},
{"cat":1673007}
''',
'''
{"cat":1673009},
{"cat":1672989}
''']
name = '[NAME]'
segment1 = move
segment2 = markets[0]
data = """
{
"partner_id": [PARTNER ID],
"name": "%s",
"recency": 90,
"prospecting": true,
"retargeting": true,
"segments": {
"AND": [
{
"AND": [
{
"OR": [%s]
},
{
"OR": [%s]
},
]
}
]
},
"countryCodes": [
"ALL"
],
"device_type": "all"
}
""" % (name, segment1, segment2)
#Creating the method signature
def signatureInputBuilder(url, method, data):
stringToSign = method
parsedUrl = urllib.parse.urlparse(url)
print (parsedUrl)
stringToSign += parsedUrl.path
# splitting the query into array of parameters separated by the '&' character
#print parsedUrl.query
qP = parsedUrl.query.split('&')
#print qP
if len(qP) > 0:
for qS in qP:
qP2 = qS.split('=', 1)
#print qP2
if len(qP2) > 1:
stringToSign += qP2[1]
#print stringToSign
if data != None :
stringToSign += data
print ("\nString to be Signed:\n" + stringToSign)
stringToSign_b = bytes(stringToSign, 'UTF-8')
h = hmac.new(bksecretkey_b, stringToSign_b, hashlib.sha256)
s = base64.standard_b64encode(h.digest())
print ("\nRaw Method Signature:\n" + s.decode("utf-8"))
u = urllib.parse.quote_plus(s)
print ("\nURL Encoded Method Signature (bksig):\n" + u)
newUrl = url
if url.find('?') == -1 :
newUrl += '?'
else:
newUrl += '&'
newUrl += 'bkuid=' + bkuid + '&bksig=' + u
return newUrl
#Generating the method request
def doRequest(url, method, data):
cJ = http.cookielib.CookieJar()
request = None
if method == 'PUT':
request = urllib2.Request(url, data, headers)
request.get_method = lambda: 'PUT'
elif method == 'DELETE' :
request = urllib.request(url, data, headers)
request.get_method = lambda: 'DELETE'
elif data != None :
request = urllib.request(url, data, headers)
else:
request = urllib.request(url, None, headers)
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cJ))
u = opener.open(request)
rawData = u.read()
print ("\nResponse Code: 200")
print ("\nAPI Response:\n" + rawData + "\n")
return rawData
#4. Specify the API request method
def main(argv=None):
# Select the API Method by uncommenting the newUrl reference variable and doRequest() method
# GET
#newUrl = signatureInputBuilder(Url, 'GET', None)
#r = requests.get(newUrl)
# POST
newUrl = signatureInputBuilder(Url, 'POST', data)
print(newUrl)
r = requests.post(newUrl, data = data, headers=headers)
print(r.raise_for_status())
# PUT
#newUrl = signatureInputBuilder(Url, 'PUT', data)
#r = requests.put(newUrl, data = data)
#DELETE
#newUrl = signatureInputBuilder(Url, 'DELETE', None)
#r = requests.delete(newUrl)
print ("API Call: \n" + newUrl)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment