Google Ads API JS Client Library
An unofficial JS client library for the SOAP-based DFP Ads API.
Guides & samples
To get started quickly, use one of the step by step guides above for the environment of your choice. Otherwise, below is a general walkthrough of using this library.
Setup
Follow steps 1 - 3 of the Authentication guide to create a Google API Console project, generate your OAuth2 credentials, and configure your DFP network
- Service account: Securely store the downloaded private key in a location accessible to your node project. The project will need it to make authorized API calls.
- Web application: Securely store the client ID and client secret in a location accessible to your node project. You will need them to make authorized API calls.
Use server-side
-
Option A: Install using NVM (see here for installing NVM)
$ nvm install node
Option B: Install from nodejs.org
-
Initialize a node project
$ npm init -y
-
Install this client library as a dependency
$ npm install -S gads
-
(Optional) Setup Typescript for your node project
$ npm install -D typescript @types/node $ node ./node_modules/typescript/lib/tsc --init --lib es6 --outDir build --strictNullChecks false
-
Include an authentication library of your choice that implements the OAuth2 service account or web app flow. A good option is the Google APIs Node.js Client, made for OAuth2 authorization and authentication with Google APIs.
$ npm install -S google-auth-library
-
Implement the GoogleOAuth2Client interface using your authentication library. The interface should be implemented to meet your environment, OAuth2 workflow, and application requirements
- Service account: See GoogleOAuth2ServiceClient
- Web application: See GoogleOAuth2WebAppClient
-
Initialize your GoogleOAuth2Client with your OAuth2 credentials
-
Service account
Note: Make sure your credentials are accessible to your project (see here for details)
;
- Web application// ...;
-
-
Initialize a DFP Client
// ...; -
Construct a SOAP client to a service and perform an operation. See the DFP API reference docs for available services / operations
- Using callbacks
// Create a network servicedfpClient.getService'NetworkService',- Using promises
// Create a network servicedfpClient.getService'NetworkService'// Request the current network and get the response.thenservice.getCurrentNetwork// Print out some network information.then;- Using async / await
// Create a network service;// Request the current network and get the response;// Print out some network information;console.log`Current network has network code `;console.log`Current network has display name ""`;
Use client-side
-
Download the minified library (from here) and include it on your site
-
Include an authentication library of your choice that implements the OAuth2 web app flow. A good option is the Google JS Client library, made for OAuth2 authorization and authentication with Google APIs. See here for an example
-
Implement the GoogleOAuth2Client interface using your authentication library. The interface should be implemented to meet your environment, OAuth2 workflow, and application requirements
- Web application: See GoogleOAuth2WebAppClient
-
Setup a proxy server & initialize a DFP Proxy
Note: Not required for Chrome extensions. See here for more details
DFP API servers are not currently setup for CORS, so a proxy must be used to forward the HTTPS SOAP requests. You can choose to use a trusted proxy or implement one yourself. See here for a simple example
const proxy =hostname: proxyHostname //e.g. localhostport: proxyPort // e.g. 80path: proxyPath // e.g. /foo/barprotocol: proxyPortocol // e.g. http or https; -
Initialize your GoogleOAuth2Client with your OAuth2 credentials
// ...const oauth2Client = gapi; -
Initialize a DFP Client
// ...const dfpClient = oauth2Client applicationName networkCode cache proxy /*, ...*/; -
Construct a SOAP client to a service and perform an operation. See the DFP API reference docs for available services / operations
- Using callbacks
// Create a network servicedfpClient- Using promises
// Create a network servicedfpClient// Request the current network and get the response// Print out some network information;- Using async / await
// Create a network serviceconst service = await dfpClient// Request the current network and get the responseconst res = await service;// Print out some network informationconst network = resrval;console;console;
Getting support
For client library specific bug reports and patches, please create an issue on the issue tracker.
For general DFP API questions, bug reports, or feature requests, please post to the official API forums:
Useful References
- The DFP API Reference Guide for Developers