This is an alpha version of the Pyhon library for generating a Mastercard API compliant OAuth signature. As such it is a work-in-progress, not for commercial use, and unsupported. This warning will be removed when the bundle is finalised.
Python 3.7.x
Before using this library, you will need to set up a project in the Mastercard Developers Portal.
As part of this set up, you'll receive credentials for your app:
- A consumer key (displayed on the Mastercard Developer Portal)
- A private request signing key (matching the public certificate displayed on the Mastercard Developer Portal)
pip3 install git+https://github.com/Mastercard/oauth1-signer-python.git
git clone https://github.com/Mastercard/oauth1-signer-python.git
Change to the repo folder, and enter :
python3 setup.py install
The following code snippets show how to use this signing library to send messages to a Mastercard service.
import sys
import requests
from oauth1.oauth import OAuth
import oauth1.authenticationutils as authenticationutils
import json
from urllib.parse import urlencode
Get a signing key from the .p12 file (replace place-holder strings with values from your project in developer zone).
signing_key = authenticationutils.load_signing_key('your-keyFile.p12', 'the-keystore-password')
consumer_key = 'your-consumer-key-from-devzone'
baseUrl = 'https://sandbox.api.mastercard.com' # remove 'sandbox.' if calling production
queryMap = {
"Format": "XML", # change this to toggle between and XML or JSON response
"Details": "offers.easysavings",
"PageOffset": "0",
"PageLength": "5",
"Latitude": "38.53463",
"Longitude": "-90.286781"
}
uri = baseUrl + "/merchants/v1/merchant?" + urlencode(queryMap)
header = OAuth().get_authorization_header(uri, 'GET', None, consumer_key, signing_key)
headers = {'Authorization': header, 'Content-Type': 'application/json'}
r = requests.get(uri, headers=headers)
print(r.text)
uri = baseUrl + "/eop/offer/v1/search?Format=XML" # change this to toggle between and XML or JSON response
reqBodyMap = {
'OfferSearchCriteria': {
'ItemsPerPage': 300,
'Program': 'easysavings'
}
}
reqJson = json.dumps(reqBodyMap)
header = OAuth().get_authorization_header(uri, 'POST', reqJson, consumer_key, signing_key)
headers = {'Authorization': header, 'Content-Type': 'application/json'}
r = requests.post(uri, headers=headers, data=reqJson)
print(r.text)