Convert API authorization using Python

In order to use Convert's API, developers would first need to connect and authorize themselves. The code below is an example of how this can be done using Python.

from datetime import datetime
import hmac
import hashlib
import base64
import requests
import binascii

ApplicationID = 'AAAAAAA'; #Replace it with your APP ID
ApplicationSecretKey = 'SSSSSSS'; #Replace it with your APP SECRET KEY

Expires = 30; # In seconds. Request lifetime. If your requests take much time, try to increase this value

RequestURL = "https://app.convert.com/api/v1/projects/";
RequestBody = '';

ExpiresTimestamp = int(datetime.now(tz=None).timestamp()) + Expires;

SignString = ApplicationID + "\n" + str(ExpiresTimestamp) + "\n" +RequestURL + "\n" +RequestBody;

message = bytes(SignString, 'utf-8')
secret = bytes(ApplicationSecretKey, 'utf-8')

signature = str(binascii.hexlify(hmac.new(secret, message, digestmod=hashlib.sha256).digest()),'ascii')
Authorization = "Convert-HMAC-SHA256 Signature=" + signature;

headers={"Expires": ExpiresTimestamp, "Convert-Authorization": Authorization, "Convert-Application-ID": ApplicationID}
r = requests.get(RequestURL,headers=headers)

print(r.json())

 

Need assistance? Start a conversation with our Support Team.
CONVERT MORE

Reliably Convert More Site Visitors, Even If Everything Else is Changing in Your Business.

START 15-DAY FREE TRIAL
✓ No Credit Card  ✓ Easy Set-Up  ✓ Access All Features

Comments

  • Avatar
    Eugene Pyvovarov

    there are few errors in this sample code:
    1) RequestURL = "https://app.convert.com/api/v1/projects/"; should be api not app
    2) "Convert-Authorization" in the header should be just "Authorization"
    3) headers={"Expires": ExpiresTimestamp - here you should change ExpiresTimestamp to str(ExpiresTimestamp)