Skip to content

mickfGit/oauth1-signer-python-1

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

oauth1-signer-python

Table of Contents

Overview

This is the Python version of the Mastercard compliant OAuth signature libraries.

Compatibility

Python 3.6, 3.7

References

Usage

Prerequisites

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)

Adding the Library to Your Project

PIP

pip install mastercard-oauth1-signer

or Clone

git clone https://github.com/Mastercard/oauth1-signer-python.git

Change to the repo folder, and enter :

python3 setup.py install

Sample Code

The following code snippets show how to use this signing library to send messages to a Mastercard service.

Imports needed for the code snippets.
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-developer.mastercard.com'

baseUrl = 'https://sandbox.api.mastercard.com' # remove 'sandbox.' if calling production
To send a GET with query parameters:
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)
To send a POST to :
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)

About

Python library for generating a Mastercard API compliant OAuth signature - In development, not for commercial use

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 100.0%