[ google dfp ] ruby client

Installation

gem install google-dfp-api

New Object

dfp = DfpApi::Api.new

This will read the configuration from ~/dfp_api.yml

---
# This is an example configuration file for the Ruby DFP API client library.
# Please fill in the required fields, and copy it over to your home directory.
# Detailed descriptions of these properties can be found at:
# https://developers.google.com/doubleclick-publishers/docs/soap_xml
:authentication:
# Authentication method, methods currently supported:
# OAUTH2, OAUTH2_SERVICE_ACCOUNT
:method: OAUTH2_SERVICE_ACCOUNT

# Auth parameters for OAUTH2_SERVICE_ACCOUNT method. See:
# https://developers.google.com/accounts/docs/OAuth2ServiceAccount
# You can provide path to a file with 'oauth2_keyfile' or the key itself with
# 'oauth2_key' option.
:oauth2_keyfile: INSERT_OAUTH2_KEYFILE_HERE
# Specify the issuer only if you are using a key directly with no key file.
#:oauth2_key: INSERT_OAUTH2_KEY_HERE
#:oauth2_issuer: INSERT_OAUTH2_ISSUER_HERE
# To impersonate a user set prn to an email address.
#:oauth2_prn: INSERT_OAUTH2_PRN_HERE

# Auth parameters for OAUTH2 method.
# Set the OAuth2 client id and secret. Register your application here to
# obtain these values:
# https://console.developers.google.com/
#:oauth2_client_id: INSERT_OAUTH2_CLIENT_ID_HERE
#:oauth2_client_secret: INSERT_OAUTH2_CLIENT_SECRET_HERE
# Optional, see: https://developers.google.com/accounts/docs/OAuth2WebServer
#:oauth2_callback: INSERT_OAUTH2_CALLBACK_URL_HERE
#:oauth2_state: INSERT_OAUTH2_STATE_HERE
#:oauth2_access_type: INSERT_OAUTH2_ACCESS_TYPE_HERE
#:oauth2_prompt: INSERT_OAUTH2_PROMPT_HERE
# You can define extra scopes so that you can reuse your refresh token for
# other APIs.
#:oauth2_extra_scopes: [INSERT_EXTRA_SCOPES_HERE]

# Other parameters.
:application_name: INSERT_APPLICATION_NAME_HERE

# Required for all calls other than GetAllNetworks and MakeTestNetwork.
:network_code: INSERT_NETWORK_CODE_HERE
:service:
# Only production environment is now available.
:environment: PRODUCTION
:connection:
# Enable to request all responses to be compressed.
:enable_gzip: false
# If your proxy connection requires authentication, make sure to include it in
# the URL, e.g.: http://user:password@proxy_hostname:8080
#:proxy: INSERT_PROXY_HERE
:library:
:log_level: INFO

We can specify the `​dfp_api.yml` filepath ( ref )

dfp = DfpApi::Api.new(filename)

Example code to get all CustomTargeting

require 'dfp_api'
dfp = DfpApi::Api.new

API_VERSION = :v201705

# Get the CustomTargetingService.
custom_targeting_service = dfp.service(:CustomTargetingService, API_VERSION)

########################################################
# Create a statement to get all custom targeting keys.
statement = DfpApi::FilterStatement.new('ORDER BY id DESC')
p statement.toStatement()

# Get custom targeting keys by statement.
page = custom_targeting_service.get_custom_targeting_keys_by_statement(
statement.toStatement())
keys = page[:results]

raise 'No targeting keys found to update' if keys.blank?

# Update each local custom targeting key object by changing its display name.
keys.each do |key|
display_name = (key[:display_name].nil?) ? key[:name] : key[:display_name]
puts display_name
puts key
end

ref : https://developers.google.com/doubleclick-publishers/docs/reporting

More Example

https://github.com/googleads/google-api-ads-ruby/tree/master/dfp_api/examples/v201705/custom_targeting_service

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s