GuidesAPI ReferenceChangelogDiscussions
GuidesAPI ReferenceChangelogLog In

Discussions

Ask a Question
Back to All

Response 403 for create campaign

I am trying to create a campaign for my client by python api, but I got response 403 with no information.
Could you help me with this? the authorization method is right, I used it to push audience and it is correct.

here is my code:
import requests
import json
import time
from creds_and_args import CLIENT_ID, CLIENT_SECRET, NEW_CAMPAIGN_NAME, ACCOUNT_ID

Set the API endpoint URL

api_endpoint = "https://api.criteo.com/2022-10/retail-media/accounts/" + ACCOUNT_ID + "/campaigns"

Get the Client Auth Token

def getToken():
# ================= Get access_token =================
url_token = "https://api.criteo.com/oauth2/token"
# Use the client_id & client_secret from Criteo Developer Portal
# You can modified CLIENT_ID & CLIENT_SECRET in cred_iminit.py
payload = f"grant_type=client_credentials&client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}%2BRxUK6"
headers = {
"accept": "application/json",
"content-type": "application/x-www-form-urlencoded"
}
rsp_token = requests.post(url_token, data=payload, headers=headers)
rsp_token = rsp_token.json()
access_token = rsp_token['access_token']
token_type = rsp_token['token_type']
# print("ACCESS_TOKEN: ", str(access_token))
auth = str(token_type) + " " + str(access_token)
return auth

if name == 'main':
# Get the Client Auth Token and print it
Auth = getToken()
print(Auth)

# Set the campaign configuration
payload = json.dumps({
    "data": {
        "type": "RetailMediaCampaign",
        "attributes": {
            "name": NEW_CAMPAIGN_NAME,
            "type": "auction",
            "clickAttributionWindow": "30D",
            "budget": "2.00",
            "clickAttributionScope": "samesku"
        }
    }
})
headers = {
   # 'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': Auth
}

# print(api_endpoint)
# print(headers)
# print(payload)
# Make the API request
response = requests.request("GET", api_endpoint, headers=headers, data={})

# Check the response status code
if response.status_code == 201:
    # Campaign was created successfully
    campaign_id = response.json()["id"]
    print(f"Campaign created with ID: {campaign_id}")
else:
    # There was an error creating the campaign
    print("Error creating campaign: ")
    print(response)

I got:
Error creating campaign:
<Response [403]>