我想查询 Criteo REST API v2。
下面的答案表明 Criteo 尚未实现 REST API v2,因此我改用 v1。他们的网站说 v1 将很快被弃用。
API v1 在这里描述
API v1 的 Swagger 在这里
我真正想使用的 REST API v2在这里
这是我此时的代码:
#! /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