1

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
markhorrocks
  • 1,199
  • 19
  • 82
  • 151

1 Answers1

3

Looks like you need to add the following to your headers:

'Authorization': 'Bearer VALID_JWT_TOKEN_BASE64'

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):

curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer [token]' https://api.criteo.com/marketing/v1/portfolio

That at least gave me an unauthorized response. Try updating your uri to:

uri = URI('https://api.criteo.com/marketing/v1/portfolio')
codenamev
  • 2,203
  • 18
  • 24
  • I edited the question to reflect your suggestion about the auth header and tried a variety of endpoints including your suggestions but no luck. – markhorrocks Dec 14 '18 at 17:54
  • @markhorrocks I updated my answer after testing their endpoints. Looks like they haven't quite made the transition to v2 of their API. – codenamev Dec 14 '18 at 20:04
  • This is the answer. [Always read the API documentation first](https://api.criteo.com/marketing/swagger/ui/index#!/Portfolio/Portfolio_GetPortfolio) which includes the Request URL `https://api.criteo.com/marketing/v1/portfolio`. – anothermh Dec 17 '18 at 04:34
  • The question was how to use the REST API v2. I had read the documentation many times but the answer above indicated that v2 is not yet implemented so I resorted to v1 in the interim. – markhorrocks Dec 17 '18 at 18:27