Get Started Using Java with Bing Ads API
You will need user credentials with access to Microsoft Advertising either in production or sandbox. For the production environment you will need a production developer token. All sandbox clients can use the universal sandbox developer token i.e., BBD37VB98. For more information, please see Get Started With the Bing Ads API and Sandbox.
To authenticate via OAuth, you must also register an application and get the corresponding client identifier. You also need to take note of the client secret and redirect URI if you are developing a web application. For more details about registering an application in production and the authorization code grant flow, see Authentication with OAuth and Authentication With the SDKs. Although in production you must use your own application ID (a.k.a. client ID), all Microsoft Advertising customers can use the public "Tutorial Sample App" client ID in sandbox i.e., 4c0b021c-00c3-4508-838f-d3127e8167ff. For more information, please see Sandbox.
Install the SDK
When you create a Maven project and include the microsoft.bingads Maven artifact as shown below, additional dependencies are installed automatically. If you are not using a Maven project, you must include the correct version of each dependency. For more information, please see the Walkthrough: Bing Ads API Web Application in Java or Walkthrough: Bing Ads API Desktop Application in Java application examples.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> ... <dependencies> <dependency> <groupId>com.microsoft.bingads</groupId> <artifactId>microsoft.bingads</artifactId> <version>13.0.10</version> </dependency> </dependencies> </project>
Version 13.0.10 is included as an example. For details about the latest SDK dependency version, please see the Bing Ads Java SDK GitHub README.md.
Once you have the Bing Ads Java SDK installed, you can either browse the Bing Ads API Code Examples, download the examples from GitHub, or follow one of the application walkthroughs for a Web or Desktop application.
To use the Sandbox environment, create a new text file named bingads.properties within your project source root directory e.g. ProjectName\src\bingads.properties and add the following text. The following are the complete contents of the bingads.properties file. If the sandbox environment setting is malformed or missing, the default environment is production.
You can also set the environment for each ServiceClient individually as follows.
CustomerService = new ServiceClient<ICustomerManagementService>( authorizationData, ApiEnvironment.SANDBOX, ICustomerManagementService.class );
Whether you set the ServiceClient environment globally or individually, separately you'll also need to set the OAuth environment to sandbox.
final OAuthDesktopMobileAuthCodeGrant oAuthDesktopMobileAuthCodeGrant = new OAuthDesktopMobileAuthCodeGrant( ClientId, ApiEnvironment.SANDBOX );
Web applications with a client secret and custom redirect URI should use OAuthWebAuthCodeGrant as follows.
OAuthWebAuthCodeGrant oAuthWebAuthCodeGrant = new OAuthWebAuthCodeGrant( ClientId, ClientSecret, new URL(RedirectUri), ApiEnvironment.SANDBOX );