| 9005 |
manish.sha |
1 |
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
|
2 |
//
|
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
|
5 |
// You may obtain a copy of the License at
|
|
|
6 |
//
|
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
8 |
//
|
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
12 |
// See the License for the specific language governing permissions and
|
|
|
13 |
// limitations under the License.
|
|
|
14 |
|
|
|
15 |
package adwords.axis.v201302.campaignmanagement;
|
|
|
16 |
|
|
|
17 |
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
|
|
|
18 |
import com.google.api.ads.adwords.axis.v201302.cm.Address;
|
|
|
19 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignAdExtension;
|
|
|
20 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignAdExtensionOperation;
|
|
|
21 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignAdExtensionReturnValue;
|
|
|
22 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignAdExtensionServiceInterface;
|
|
|
23 |
import com.google.api.ads.adwords.axis.v201302.cm.GeoLocation;
|
|
|
24 |
import com.google.api.ads.adwords.axis.v201302.cm.GeoLocationSelector;
|
|
|
25 |
import com.google.api.ads.adwords.axis.v201302.cm.GeoLocationServiceInterface;
|
|
|
26 |
import com.google.api.ads.adwords.axis.v201302.cm.LocationExtension;
|
|
|
27 |
import com.google.api.ads.adwords.axis.v201302.cm.LocationExtensionSource;
|
|
|
28 |
import com.google.api.ads.adwords.axis.v201302.cm.Operator;
|
|
|
29 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
30 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
31 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
32 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* This example adds a location ad extension to a campaign for a location
|
|
|
36 |
* obtained from the GeoLocationService. To get campaigns, run
|
|
|
37 |
* GetCampaigns.java.
|
|
|
38 |
*
|
|
|
39 |
* Tags: GeoLocationService.get
|
|
|
40 |
* Tags: CampaignAdExtensionService.mutate
|
|
|
41 |
*
|
|
|
42 |
* Category: adx-exclude
|
|
|
43 |
*
|
|
|
44 |
* @author Kevin Winter
|
|
|
45 |
*/
|
|
|
46 |
public class AddLocationExtension {
|
|
|
47 |
|
|
|
48 |
public static void main(String[] args) throws Exception {
|
|
|
49 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
50 |
// and can be used in place of a service account.
|
|
|
51 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
52 |
.forApi(Api.ADWORDS)
|
|
|
53 |
.fromFile()
|
|
|
54 |
.build()
|
|
|
55 |
.generateCredential();
|
|
|
56 |
|
|
|
57 |
// Construct an AdWordsSession.
|
|
|
58 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
59 |
.fromFile()
|
|
|
60 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
61 |
.build();
|
|
|
62 |
|
|
|
63 |
long campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE");
|
|
|
64 |
|
|
|
65 |
AdWordsServices adWordsServices = new AdWordsServices();
|
|
|
66 |
|
|
|
67 |
runExample(adWordsServices, session, campaignId);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
public static void runExample(
|
|
|
71 |
AdWordsServices adWordsServices, AdWordsSession session, Long campaignId) throws Exception {
|
|
|
72 |
// Get the CampaignAdExtensionService.
|
|
|
73 |
CampaignAdExtensionServiceInterface campaignAdExtensionService =
|
|
|
74 |
adWordsServices.get(session, CampaignAdExtensionServiceInterface.class);
|
|
|
75 |
|
|
|
76 |
// Get the GeoLocationService.
|
|
|
77 |
GeoLocationServiceInterface geoLocationService =
|
|
|
78 |
adWordsServices.get(session, GeoLocationServiceInterface.class);
|
|
|
79 |
|
|
|
80 |
// Create address.
|
|
|
81 |
Address address = new Address();
|
|
|
82 |
address.setStreetAddress("1600 Amphitheatre Parkway");
|
|
|
83 |
address.setCityName("Mountain View");
|
|
|
84 |
address.setProvinceCode("US-CA");
|
|
|
85 |
address.setPostalCode("94043");
|
|
|
86 |
address.setCountryCode("US");
|
|
|
87 |
|
|
|
88 |
// Create geo location selector.
|
|
|
89 |
GeoLocationSelector selector = new GeoLocationSelector();
|
|
|
90 |
selector.setAddresses(new Address[] {address});
|
|
|
91 |
|
|
|
92 |
// Get geo location.
|
|
|
93 |
GeoLocation[] geoLocationResult = geoLocationService.get(selector);
|
|
|
94 |
GeoLocation geoLocation = geoLocationResult[0];
|
|
|
95 |
|
|
|
96 |
// Create location extension.
|
|
|
97 |
LocationExtension locationExtension = new LocationExtension();
|
|
|
98 |
locationExtension.setAddress(geoLocation.getAddress());
|
|
|
99 |
locationExtension.setGeoPoint(geoLocation.getGeoPoint());
|
|
|
100 |
locationExtension.setEncodedLocation(geoLocation.getEncodedLocation());
|
|
|
101 |
locationExtension.setSource(LocationExtensionSource.ADWORDS_FRONTEND);
|
|
|
102 |
|
|
|
103 |
// You can optionally provide these field(s).
|
|
|
104 |
locationExtension.setCompanyName("Google");
|
|
|
105 |
locationExtension.setPhoneNumber("650-253-0000");
|
|
|
106 |
|
|
|
107 |
// Create campaign ad extension.
|
|
|
108 |
CampaignAdExtension campaignAdExtension = new CampaignAdExtension();
|
|
|
109 |
campaignAdExtension.setCampaignId(campaignId);
|
|
|
110 |
campaignAdExtension.setAdExtension(locationExtension);
|
|
|
111 |
|
|
|
112 |
// Create operations.
|
|
|
113 |
CampaignAdExtensionOperation operation = new CampaignAdExtensionOperation();
|
|
|
114 |
operation.setOperand(campaignAdExtension);
|
|
|
115 |
operation.setOperator(Operator.ADD);
|
|
|
116 |
|
|
|
117 |
CampaignAdExtensionOperation[] operations = new CampaignAdExtensionOperation[] {operation};
|
|
|
118 |
|
|
|
119 |
// Add campaign ad extension.
|
|
|
120 |
CampaignAdExtensionReturnValue result = campaignAdExtensionService.mutate(operations);
|
|
|
121 |
|
|
|
122 |
// Display campaign ad extensions.
|
|
|
123 |
for (CampaignAdExtension campaignAdExtensionResult : result.getValue()) {
|
|
|
124 |
System.out.println("Location campaign ad extension with campaign id \""
|
|
|
125 |
+ campaignAdExtensionResult.getCampaignId() + "\", ad extension id \""
|
|
|
126 |
+ campaignAdExtensionResult.getAdExtension().getId() + "\", and type \""
|
|
|
127 |
+ campaignAdExtensionResult.getAdExtension().getAdExtensionType() + "\" was added.");
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
}
|