Playing with Ibanity (continued)¶
Tuesday, January 14, 2025
I asked in the Ibanity support forum what’s wrong with my yesterday’s script and Pedro gave me a more promising version:
from pathlib import Path
import requests
import base64
import sys
# Define client ID and secret
client_id = "69d85961-7d68-474d-9ac2-426fdc71bab8"
client_secret = "valid_client_secret"
# Define certificate and key file paths
cert_dir = Path(f"~/Documents/ibanity/application_{client_id}/").expanduser()
cert_file = cert_dir / "certificate.pem"
key_file = cert_dir / "decrypted_private_key.pem"
# Validate if the key and certificate files exist
if not cert_file.exists():
print(f"Error: Certificate file not found at {cert_file}")
sys.exit(1)
if not key_file.exists():
print(f"Error: Private key file not found at {key_file}")
sys.exit(1)
# Base64 encode client_id and client_secret for Basic Auth
client_credentials = f"{client_id}:{client_secret}"
encoded_credentials = base64.b64encode(client_credentials.encode()).decode()
# Create an HTTPS session
s = requests.Session()
try:
# Attach client certificate and key to the session
s.cert = (cert_file, key_file)
# Add the Authorization header
headers = {
"Authorization": f"Basic {encoded_credentials}",
"Content-Type": "application/x-www-form-urlencoded", # Required for OAuth2 requests
}
# Make the POST request
response = s.post(
"https://api.ibanity.com/einvoicing/oauth2/token",
data={"grant_type": "client_credentials"},
headers=headers
)
# Handle response
print(f"Response status: {response.status_code}")
print(f"Response body: {response.text}")
except requests.exceptions.SSLError as ssl_error:
print(f"SSL Error: {ssl_error}")
print("Please ensure your certificate and private key are correct and properly configured.")
except requests.exceptions.RequestException as req_error:
print(f"Request Error: {req_error}")
Great! This version works:
Response status: 200
Response body: {"access_token":"access_token_1736875832","expires_in":300,"not-before-policy":0,"refresh_expires_in":300,"refresh_token":"refresh_token_1736875832","scope":"email profile","session_state":"94bdf4ce-0dea-4ab7-abda-a53c7ba4bc87","token_type":"bearer"}
TIL that providing my credentials is more complex than just giving a certificate and a private key, it also requires Basic access authentication.
I continue to read here: https://documentation.ibanity.com/einvoicing/1/products#services
Some sentences in this document make me believe that maybe our customers can access the Peppol network via their online banking application: “All the customer has to do is register to Zoomit via his preferred online banking application.” So there is some hope that our customers can simply instruct their bank to accept incoming purchase invoices, they would then continue to enter them manually into TIM as they are used to do. Making this step automatically would involve considerable changes in configuration, we don’t want this right now.
TIM can already generate an invoice or credit note into Peppol XML format. The next steps TIM should learn to do:
Customer Search – check if a customer is registered on the Peppol network (It seems that the sender is responsible for verifying this, which makes sense)
Send document - sending the invoice or credit note in XML format