Skip to content

Instantly share code, notes, and snippets.

@gmjelle
Last active November 20, 2016 15:22
Show Gist options
  • Save gmjelle/b99f8d895cb9e70b53022c2a5df5ab65 to your computer and use it in GitHub Desktop.
Save gmjelle/b99f8d895cb9e70b53022c2a5df5ab65 to your computer and use it in GitHub Desktop.
Bol Plaza API
import time
import requests
import hmac
import hashlib
import base64
SECRET_KEY='XXX'
PUBLIC_KEY='YYY'
code = '123' # I was thinking of using a unique md5 hash for each product, but for this example I'll just use 123
content_type = 'application/xml' # I have also used "application/xml; charset=UTF-8"
date = time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime( int(time.time() + 3600))) # I have to add 3600 seconds or else the API sends back a "Client and server signature do not match" error
method = 'POST'
uri = '/offers/v1/%s' % code # the endpoint
# Let's make the Hash. This is working fine, because I have no problems with the other API endpoints signature = PUBLIC_KEY.encode('utf-8') + b':' + b64
headers = {'Content-Type': 'application/xml',
'X-BOL-Date': date,
'X-BOL-Authorization': signature}
msg = """{method}
{content_type}
{date}
x-bol-date:{date}
{uri}""".format(content_type=content_type,
date=date,
method=method,
uri=uri)
h = hmac.new(
SECRET_KEY.encode('utf-8'),
msg.encode('utf-8'), hashlib.sha256)
b64 = base64.b64encode(h.digest())
# create the signature for the headers
signature = PUBLIC_KEY.encode('utf-8') + b':' + b64
# the headers for the call
headers = {'Content-Type': 'application/xml',
'X-BOL-Date': date,
'X-BOL-Authorization': signature}
# I have tried "OfferCreate" as well as "api:OfferCreate" as suggested by the XSD to XML generator in IntelliJ Idea.
xml_to_send="""<?xml version="1.0" encoding="UTF-8"?>
<OfferCreate xmlns="http://plazaapi.bol.com/offers/xsd/api-1.0.xsd">
<EAN>01</EAN>
<Condition>NEW</Condition>
<Price>100.00</Price>
<DeliveryCode>3-5d</DeliveryCode>
<QuantityInStock>3</QuantityInStock>
<Publish>false</Publish>
<ReferenceCode></ReferenceCode>
<Description></Description>
</OfferCreate>"""
# make a request back to the endpoint with the code ('123')
r = requests.post('https://plazaapi.bol.com/offers/v1/%s' % code, data=xml_to_send, headers=headers)
#######
#
# The error I get:
"""
Unauthorized
401
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ServiceError xmlns="http://plazaapi.bol.com/offers/xsd/api-1.0.xsd"><ErrorCode>41201</ErrorCode><ErrorMessage>Request contains invalid authentication headers</ErrorMessage></ServiceError>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ServiceError xmlns="http://plazaapi.bol.com/offers/xsd/api-1.0.xsd"><ErrorCode>41201</ErrorCode><ErrorMessage>Request contains invalid authentication headers</ErrorMessage></ServiceError>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment