Hi, I've been trying to get my x-pay tokens to authorise using the helloworld demo in python.
I've gone through all the checks that have been suggested and still am getting the following response.
{"responseStatus":{"status":401,"code":"9159","severity":"ERROR","message":"Token Validation Failed","info":""}}'
I have simply copied the helloworld.py code provided and updated keys... (have removed the API and shared secret key for safety though here). Can you advise?
import datetime
import hashlib
import hmac
import json
import logging
import time
from calendar import timegm
import requests
# DEBUG = False
DEBUG = True
# helloworld
# Sample Code for API Key- Shared Secret (X-Pay-Token)
# These two lines enable debugging at httplib level (requests->urllib3->http.client)
# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# The only thing missing will be the response.body which is not logged.
try:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
http_client.HTTPSConnection.debuglevel = 0
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
print("START Sample Code for API Key- Shared Secret (X-Pay-Token)")
print(datetime.datetime.now())
date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
headers = {}
body = {}
payload = json.loads('''{}
''')
api_key = #####
shared_secret = ####
timeout = 10
query_string = "apiKey=" + api_key
resource_path = "helloworld"
body = ""
secret = bytes(shared_secret, 'utf-8')
timestamp = str(timegm(datetime.datetime.now(datetime.timezone.utc).timetuple()))
pre_hash_string = bytes(timestamp + resource_path + query_string + body, 'utf-8')
print(pre_hash_string)
hash_string = hmac.new(secret, pre_hash_string, digestmod=hashlib.sha256).hexdigest()
x_pay_token = 'xv2:' + timestamp + ':' + hash_string
print(x_pay_token)
headers = {'x-pay-token': x_pay_token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
try:
response = requests.get(url + '?' + query_string,
headers=headers,
timeout=timeout
# if DEBUG: print (response.text)
)
except Exception as e:
print(e)
if DEBUG: print(response.headers)
if DEBUG: print(response.content)
# var1 = str(response.status_code)
# var2 = '200'
# msg = " API Key- Shared Secret (X-Pay-Token) test failed"
# assert var1 == var2, msg
# print("END Sample Code for API Key- Shared Secret (X-Pay-Token)\n\n")