I want to query the Criteo REST API v2.
The answer below indicates that the REST API v2 has not yet been implement by Criteo so i have used v1 instead. Their website says that v1 will be deprecated soon.
The API v1 is described here
The Swagger for API v1 is here
The REST API v2 which I'd really like to use is here
Here is my code at this point:
#! /usr/bin/ruby
require 'net/http'
require 'net/https'
require 'uri'
require 'jwt'
require 'date'
require 'json'
CLIENT_ID = 'mapi-XXX'
CLIENT_SECRET = 'YYY'
end_date = Date.today
start_date = Date.today - Date.today.mday + 1
# first get a valid token
uri = URI('https://api.criteo.com/marketing/oauth2/token')
headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' }
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.path, headers)
request.set_form_data( 'client_id' => CLIENT_ID, 'client_secret' => CLIENT_SECRET, 'grant_type' => 'client_credentials' )
response = https.request(request)
access_token = JSON.parse(response.body)['access_token']
puts access_token
uri = URI('https://api.criteo.com/marketing/v1/portfolio')
headers = { 'Accept': 'application/json', 'Authorization': "Bearer #{access_token}" }
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.path, headers)
response = https.request(request)
puts response.body
uri = URI('https://api.criteo.com/marketing/v1/campaigns?campaignStatus=Running')
headers = { 'Accept': 'application/json', 'Authorization': "Bearer #{access_token}" }
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.path, headers)
response = https.request(request)
puts response.body
json = JSON.parse(response.body)
campaigns = json.map { |campaign| campaign['campaignId'] }
# get statistics
puts start_date # => "2018-12-01"
puts end_date # => "2018-12-17"
uri = URI.parse('https://api.criteo.com/marketing/v1/statistics')
headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json', 'Authorization': "Bearer #{access_token}" }
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.path, headers)
request.set_form_data( 'reportType' => 'CampiagnPerformance', 'ignoreXDevice' => true, 'startDate' => "#{start_date}", 'endDate' => "#{end_date}", 'dimensions' => ['CampaignId'], 'metrics' => ['Clicks'], 'format' => 'Json', 'currency' => 'USD', 'timezone' => 'GMT')
#request.body = payload.to_json
puts request.body
response = https.request(request)
puts response.body
Looks like you need to add the following to your
headers
:Also, they don't appear to have migrated their
portfolio
API endpoint to v2 yet. I was able to resolve their old v1 endpoint (based on their documentation):That at least gave me an unauthorized response. Try updating your uri to: