Skip to content

Instantly share code, notes, and snippets.

@egbertp
Created February 20, 2013 14:44
Show Gist options
  • Save egbertp/4996006 to your computer and use it in GitHub Desktop.
Save egbertp/4996006 to your computer and use it in GitHub Desktop.
This ruby script connects to the Platform161 API. I've used this functionality to extract data from Platform161
##############################################################################
#
# Author: Egbert Pot | WebOak | www.weboak.nl
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
require "net/https"
require "uri"
require "json"
@token = nil
# This method handles all communication with the API
def post_to_api(action, parameters, token = nil)
uri = URI.parse("https://api.platform161.com/api/v2/#{action}")
http = Net::HTTP.new(uri.host, uri.port)
http.set_debug_output $stderr
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
request.add_field 'Content-Type', 'application/json'
request.add_field('Content-Length', parameters.length)
if token != nil
request.add_field('PFM161-API-AccessToken', token)
end
request.body = parameters
response = http.request(request)
end
# This method gets the token from Platform161. This token is needed
# for every API request
def authenticate
action = "access_tokens"
credentials = {
"user" => "YourUsername",
"password" => "YourPassword",
"client_id" => "YourClientID",
"client_secret" => "YourSecretKey"
}
credentials = credentials.to_json
resp_for_token = post_to_api(action, credentials)
resp_for_token = JSON.parse(resp_for_token.body)
@token = resp_for_token["token"]
end
# Call this method everytime you would like to request data from Platform161
# This method checks if the authenticaton token is already present. If not, it
# will first authenticate using the credentials above
def data_from_api(action, parameters)
authenticate unless @token != nil
post_to_api(action, parameters, @token)
end
action = "advertiser_reports"
parameters = '{"advertiser_report":{"interval":"daily","period":"last_30_days","groupings":["campaign"],"measures":["impressions","clicks","conversions","ctr"]}}'
response = data_from_api(action, parameters)
puts response.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment