Accessing AAM Rest API via Python

sabineg26801730

25-03-2019

Hi everyone,

Sabine from the Daily Telegraph here

Has anybody ever succeed in accessing the REST API with the python request library?

I have managed to get a token but I get a 403 error (logged out) with any request I attempt - however the token generated works fine with Postman/AAM Rest tool, so I'm a bit clueless as to why it doesn't work...

Looking forward to hearing about your experiences with the API!

Thanks,

Sabine

Answers (2)

Answers (2)

trevor1992

18-07-2019

Hi sabineg26801730,

I wrote this and have used it successfully in Python 3:

# Import packages

import os

import base64 as base64

import requests as requests

# Load your aam credentials (must be aam credentials, not marketing cloud credentials)

# credentials include clientID, clientSecret, username, password

aam_credentials = json.load(open('aam_credentials.json', 'r'))

# Get AAM API Token.

def getToken(clientID,clientSecret,username,password):

    oauth_request = "https://api.demdex.com/oauth/token"

    combi =  clientID+':'+clientSecret

    b64combi = base64.b64encode(combi.encode())

    base64IDSecret = b64combi.decode()

    header = {'Authorization' : 'Basic '+base64IDSecret,'Content-Type': 'application/x-www-form-urlencoded'}

    body = {'grant_type':'password','username':username,'password':password}

    audience_request = requests.post(oauth_request,headers=header,data=body)

    token = audience_request.json()['access_token']

    return token

token = getToken(clientID = aam_credentials['clientID'],

                             clientSecret = aam_credentials['clientSecret'],

                             username = aam_credentials['username'],

                             password = aam_credentials['password'])

You'll want to use that token in your other API requests until it expires, then generate a new one when your session expires.

Dylan_Kario

Employee

12-04-2019

Hi sabineg26801730,

Thanks for your question! Dylan from Audience Manager's engineering team here. Sorry to hear you've been having trouble accessing the AAM APIs with python & the requests library. Could you please share the exact API call you're making from python along with all headers?

Thanks,

Dylan