We are excited to start discussing how to use the DFP API from within the Google App Engine (Java) environment. This first blog post in the series focuses on setting up your environment and understanding how to use the API from App Engine. Future posts in this series will build upon the same project to create a sample application. Ultimately, you will be able to make DFP information more accessible within your organization, while also leveraging the ease of scale and deployability that comes with creating applications on Google App Engine.
Setting up your environment For simplicity sake we will focus on utilizing Eclipse and the Google App Engine Plugin available for Eclipse. Information about setting them up can be found here . If you prefer a different environment setup, you can always view the Getting Started Guide for App Engine for more information.Create an App Engine project You’ll need a place to do your coding so create an App Engine project in a similar manner to the screenshots below.
Note: If you would like to download all of the source code in this blog post to follow along you can do so as a tarball or you can browse the code base from our svn repository .
Dependencies The App Engine Java environment requires JAX-WS when dealing with SOAP interfaces like DFP API so you won’t be able to use the normal Java client library . You’ll need to download this client login auth file to help with authentication. Place this file in your project’s src directory. Next, you’ll need a jar and a wsdl file that we’ve compiled with JAX-WS; place them in the WEB-INF/lib and WEB-INF directory, respectively. See the files highlighted in their respective locations below.
Write some code Now that you’re set up, it’s time to write some code that uses the DFP API. This first snippet of code performs some setup for your App Engine Servlet.
/**
* Perform initialization of servlet and cached resources used to
* access DFP API.
*/
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
// Generate an authToken.
try {
authToken = regenerateAuthToken();
} catch (Exception exception) {
throw new ServletException("Could not generate an Auth Token.",
exception);
}
}
/**
* Regenerate the client login auth token that the servlet uses.
*
* @throws Exception
*/
public synchronized String regenerateAuthToken() throws Exception {
ClientLoginAuth clientLoginAuth = new ClientLoginAuth(EMAIL_ADDRESS,
EMAIL_ADDRESS_PASSWORD);
return clientLoginAuth.getAuthToken();
}
This next snippet of code handles the actual web browser requests.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException {
try {
// Retrieve an object handle to our network service.
NetworkService networkService = new NetworkService();
NetworkServiceInterface networkServiceInterface =
networkService.getNetworkServiceInterfacePort();
// Prepare header object to make server call
SoapRequestHeader requestHeader = new SoapRequestHeader();
requestHeader.setApplicationName("Hello World");
ClientLogin clientLogin = new ClientLogin();
clientLogin.setToken(authToken);
requestHeader.setAuthentication(clientLogin);
// Make protected call to the server.
String rootAdUnitId = "";
Network currentNetwork = networkServiceInterface.getCurrentNetwork(
requestHeader, null);
// Extract data from object returned from server.
rootAdUnitId = currentNetwork.getEffectiveRootAdUnitId();
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world. Your root ad unit id is: "
+ rootAdUnitId);
} catch (Exception e) {
// Perform exception handling.
e.printStackTrace();
throw new ServletException("Error occurred. Check logs for specific "
" details about the error.");
}
}
This particular code snippet retrieves the root ad unit for our network whenever someone accesses the application. You can view the full sample servlet code here .Testing To make sure your code works, let’s deploy the project locally. Right-click on your project and then choose “Run” and select the “Web Application” option from the submenu. This will deploy your code to a locally running App Engine server. The console will display the URL to access the application (usually http://localhost:8888/ ). Clicking on this link and then the subsequent servlet link should return a page similar to this.
Next Time Next time, we’ll extend the project to do some background processing and start using some other services from the DFP API.Troubleshooting We really hope you didn’t have an issue, but if you did there is a troubleshooting README file included in the sample code .
Let us know if you have any questions, and happy holidays!
- Paul Rashidi , DFP API Team