1

我想查询 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
4

1 回答 1

3

看起来您需要将以下内容添加到您的headers

'Authorization': 'Bearer VALID_JWT_TOKEN_BASE64'

此外,他们似乎还没有将他们的portfolioAPI 端点迁移到 v2。我能够解决他们的旧 v1 端点(基于他们的文档):

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

这至少给了我一个未经授权的回应。尝试将您的 uri 更新为:

uri = URI('https://api.criteo.com/marketing/v1/portfolio')
于 2018-12-14T16:03:31.743 回答