Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Accessing AAM Rest API via Python

sabineg26801730
Level 1
Level 1

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

1 Accepted Solution
Dylan_Kario
Correct answer by
Employee
Employee

Also, here's an example repo that accesses AAM APIs with python's requests library. Hope this helps point you in the right direction!

audiencemanager-api-lab/aam.py at exercise-7 · Adobe-Marketing-Cloud/audiencemanager-api-lab · GitHu...

View solution in original post

3 Replies
Dylan_Kario
Employee
Employee

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

Dylan_Kario
Correct answer by
Employee
Employee

Also, here's an example repo that accesses AAM APIs with python's requests library. Hope this helps point you in the right direction!

audiencemanager-api-lab/aam.py at exercise-7 · Adobe-Marketing-Cloud/audiencemanager-api-lab · GitHu...

View solution in original post

trevor1992
Level 2
Level 2

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.