Microsoft Advertising
A platform for Microsoft's advertising efforts designed to manage all advertising and reporting for partner advertisers. Previously known as Bing Ads and adCenter.
75 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Good morning, Team!
I'd want to request instructions on how to retrieve campaign report details using APIs.
I'm writing Python code to extract campaign details using APIs, but I'm unable to obtain the campaign dataset. As a result, I only get the HTML code of MS ads page.
I'd like to post my query here for your convenience. Please have a look and let me know what you think.
Thanks!
Query:
import requests
import snowflake.connector
import http.client as http_client
import logging
import http.client
import json
import pyodbc
# Microsoft Advertising API credentials
client_id = '********'
client_secret = '********'
refresh_token = '********'
# Snowflake credentials
snowflake_user ='********'
snowflake_password = '********'
snowflake_account = '********'
snowflake_database = '********'
snowflake_schema = '********'
snowflake_table = '********'
# Authenticate and get Microsoft Advertising access token
token_url = 'https://login.microsoftonline.com/common/oauth2/token'
token_data = {
'grant_type': 'refresh_token',
'client_id': client_id,
'client_secret': client_secret,
'refresh_token': refresh_token,
'scope': 'https://ui.ads.microsoft.com/'
}
token_response = requests.post(token_url, data=token_data)
access_token = token_response.json()['access_token']
# Fetch campaign report data from Bing Ads API
report_url = 'https://bingads.microsoft.com/Reporting/v13'
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
report_data = {
"reportName": "CampaignPerformanceReportRequest",
"aggregation": "Daily",
"columns": ["CampaignName", "Spend", "Clicks"],
"time": {
"customDateRange": {
"startDate": "2023-01-01",
"endDate": "2023-01-31"
}
}
}
report_response = requests.post(report_url, headers=headers, json=report_data)
# Connect to Snowflake
conn = snowflake.connector.connect(
user=snowflake_user,
password=snowflake_password,
account=snowflake_account,
database=snowflake_database,
schema=snowflake_schema
)
# Insert data into Snowflake table
insert_query = f"""
INSERT INTO {snowflake_table} (
CampaignName,
Spend,
Clicks
)
VALUES
(?, ?, ?)
"""
for row in report_data:
conn.cursor().execute(
insert_query,
(
row['CampaignName'],
row['Spend'],
row['Clicks']
)
)
# Close Snowflake connection
conn.close()