Mastercard OAuth1 Signer

1.9.2 · active · verified Thu Apr 16

The Mastercard OAuth1 Signer is a Python library that simplifies the process of generating OAuth 1.0a signatures required for authenticating requests to Mastercard APIs. It handles the complexities of OAuth 1.0a, including nonce generation, timestamping, and RSA-SHA256 signature creation. The current version is 1.9.2, with a release cadence focused on security updates, dependency bumps, and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `Auth` object with your Mastercard API credentials and use it with the `requests` library to make an authenticated call to a Mastercard API endpoint. Ensure you replace placeholder values with your actual `consumer_key`, `private_key_path`, `key_alias`, and `key_password`.

import requests
import os
from mastercard.oauth1.signer import Auth

# --- Environment variables or placeholder values ---
# Replace with your actual credentials or set as environment variables
CONSUMER_KEY = os.environ.get('MASTERCARD_CONSUMER_KEY', 'YOUR_CONSUMER_KEY')
PRIVATE_KEY_PATH = os.environ.get('MASTERCARD_PRIVATE_KEY_PATH', 'path/to/your/key.p12')
KEY_ALIAS = os.environ.get('MASTERCARD_KEY_ALIAS', 'keyalias') # Alias used when creating the .p12 file
KEY_PASSWORD = os.environ.get('MASTERCARD_KEY_PASSWORD', 'keypassword')
BASE_URL = os.environ.get('MASTERCARD_BASE_URL', 'https://sandbox.api.mastercard.com') # Or 'https://api.mastercard.com'

# --- Load private key and create Auth object ---
try:
    # The Auth constructor handles loading the private key from the .p12 file
    oauth_auth = Auth(
        consumer_key=CONSUMER_KEY,
        private_key_path=PRIVATE_KEY_PATH,
        private_key_password=KEY_PASSWORD,
        private_key_alias=KEY_ALIAS
    )
except Exception as e:
    print(f"Error initializing Auth: {e}")
    print("Please ensure your private key path, password, and alias are correct.")
    exit(1)

# --- Example API call (replace with your actual endpoint) ---
# This example assumes an endpoint that returns some data, like a health check or a simple resource.
# The actual endpoint will vary based on the Mastercard API you are using.
api_endpoint = f"{BASE_URL}/some/api/resource"

headers = {'Accept': 'application/json'}

try:
    response = requests.get(api_endpoint, auth=oauth_auth, headers=headers)
    response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)

    print(f"Successfully called {api_endpoint}")
    print(f"Status Code: {response.status_code}")
    print("Response Body:")
    print(response.json()) # Assuming JSON response
except requests.exceptions.RequestException as e:
    print(f"API call failed: {e}")
    if hasattr(e, 'response') and e.response is not None:
        print(f"Response Status: {e.response.status_code}")
        print(f"Response Body: {e.response.text}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →