| 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.v201309.basicoperations;
|
|
|
16 |
|
|
|
17 |
import java.util.ArrayList;
|
|
|
18 |
import java.util.HashMap;
|
|
|
19 |
import java.util.List;
|
|
|
20 |
import java.util.Map;
|
|
|
21 |
|
|
|
22 |
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
|
|
|
23 |
import com.google.api.ads.adwords.axis.v201309.cm.Campaign;
|
|
|
24 |
import com.google.api.ads.adwords.axis.v201309.cm.CampaignPage;
|
|
|
25 |
import com.google.api.ads.adwords.axis.v201309.cm.CampaignServiceInterface;
|
|
|
26 |
import com.google.api.ads.adwords.axis.v201309.cm.OrderBy;
|
|
|
27 |
import com.google.api.ads.adwords.axis.v201309.cm.Paging;
|
|
|
28 |
import com.google.api.ads.adwords.axis.v201309.cm.Selector;
|
|
|
29 |
import com.google.api.ads.adwords.axis.v201309.cm.SortOrder;
|
|
|
30 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
31 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
32 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
33 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* This example gets all campaigns. To add a campaign, run AddCampaign.java.
|
|
|
37 |
*
|
|
|
38 |
* Credentials and properties in {@code fromFile()} are pulled from the
|
|
|
39 |
* "ads.properties" file. See README for more info.
|
|
|
40 |
*
|
|
|
41 |
* Tags: CampaignService.get
|
|
|
42 |
*
|
|
|
43 |
* @author Adam Rogal
|
|
|
44 |
*/
|
|
|
45 |
public class GetCampaigns {
|
|
|
46 |
|
|
|
47 |
private static final int PAGE_SIZE = 100;
|
|
|
48 |
|
|
|
49 |
public static void main(String args[]){
|
|
|
50 |
try{
|
|
|
51 |
runExample();
|
|
|
52 |
}
|
|
|
53 |
catch(Exception e){
|
|
|
54 |
System.out.println(e.getMessage());
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public static List<Map<String,String>> runExample() throws Exception {
|
|
|
59 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
60 |
// and can be used in place of a service account.
|
|
|
61 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
62 |
.forApi(Api.ADWORDS)
|
|
|
63 |
.fromFile()
|
|
|
64 |
.build()
|
|
|
65 |
.generateCredential();
|
|
|
66 |
|
|
|
67 |
// Construct an AdWordsSession.
|
|
|
68 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
69 |
.fromFile()
|
|
|
70 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
71 |
.build();
|
|
|
72 |
|
|
|
73 |
AdWordsServices adWordsServices = new AdWordsServices();
|
|
|
74 |
|
|
|
75 |
// Get the CampaignService.
|
|
|
76 |
CampaignServiceInterface campaignService =
|
|
|
77 |
adWordsServices.get(session, CampaignServiceInterface.class);
|
|
|
78 |
|
|
|
79 |
int offset = 0;
|
|
|
80 |
|
|
|
81 |
// Create selector.
|
|
|
82 |
Selector selector = new Selector();
|
|
|
83 |
selector.setFields(new String[] {"Id", "Name", "Amount","StartDate", "EndDate", "Status","TargetGoogleSearch","TargetPartnerSearchNetwork","TargetSearchNetwork","TargetContentNetwork"});
|
|
|
84 |
selector.setOrdering(new OrderBy[] {new OrderBy("Name", SortOrder.ASCENDING)});
|
|
|
85 |
selector.setPaging(new Paging(offset, PAGE_SIZE));
|
|
|
86 |
List<Map<String,String>> campaignDataMainList = new ArrayList<Map<String,String>>();
|
|
|
87 |
CampaignPage page = null;
|
|
|
88 |
do {
|
|
|
89 |
// Get all campaigns.
|
|
|
90 |
page = campaignService.get(selector);
|
|
|
91 |
|
|
|
92 |
// Display campaigns.
|
|
|
93 |
if (page.getEntries() != null) {
|
|
|
94 |
for (Campaign campaign : page.getEntries()) {
|
| 9292 |
manish.sha |
95 |
if(campaign.getId() > 0l && campaign.getName()!=null && !("").equalsIgnoreCase(campaign.getName()) && campaign.getEndDate()!=null && !("").equalsIgnoreCase(campaign.getEndDate()) && campaign.getStartDate()!=null && !("").equalsIgnoreCase(campaign.getStartDate())){
|
|
|
96 |
System.out.println("Campaign with name \"" + campaign.getName() + "\" and id \""
|
|
|
97 |
+ campaign.getId() +"With Budget \""+ campaign.getBudget().getAmount().getMicroAmount()+ "\" was found.");
|
|
|
98 |
System.out.println("Null Comes Here");
|
|
|
99 |
Map<String,String> campaignMap = new HashMap<String,String>();
|
|
|
100 |
campaignMap.put("CampaignId", campaign.getId()+"");
|
|
|
101 |
campaignMap.put("Status", campaign.getStatus().getValue());
|
|
|
102 |
campaignMap.put("StartDate", campaign.getStartDate());
|
|
|
103 |
campaignMap.put("EndDate",campaign.getEndDate());
|
|
|
104 |
campaignMap.put("Name",campaign.getName());
|
|
|
105 |
campaignMap.put("Amount",campaign.getBudget().getAmount().getMicroAmount()+"");
|
|
|
106 |
System.out.println("Null Comes Here 2");
|
|
|
107 |
if(campaign.getNetworkSetting().getTargetGoogleSearch()!=null){
|
|
|
108 |
campaignMap.put("TargetGoogleSearch",campaign.getNetworkSetting().getTargetGoogleSearch().booleanValue()+"");
|
|
|
109 |
}
|
|
|
110 |
else{
|
|
|
111 |
campaignMap.put("TargetGoogleSearch","false");
|
|
|
112 |
}
|
|
|
113 |
if(campaign.getNetworkSetting().getTargetSearchNetwork()!=null){
|
|
|
114 |
campaignMap.put("TargetSearchNetwork",campaign.getNetworkSetting().getTargetSearchNetwork().booleanValue()+"");
|
|
|
115 |
}
|
|
|
116 |
else{
|
|
|
117 |
campaignMap.put("TargetSearchNetwork","false");
|
|
|
118 |
}
|
|
|
119 |
if(campaign.getNetworkSetting().getTargetPartnerSearchNetwork()!=null){
|
|
|
120 |
campaignMap.put("TargetPartnerSearchNetwork",campaign.getNetworkSetting().getTargetPartnerSearchNetwork().booleanValue()+"");
|
|
|
121 |
}
|
|
|
122 |
else{
|
|
|
123 |
campaignMap.put("TargetPartnerSearchNetwork","false");
|
|
|
124 |
}
|
|
|
125 |
if(campaign.getNetworkSetting().getTargetContentNetwork()){
|
|
|
126 |
campaignMap.put("TargetContentNetwork",campaign.getNetworkSetting().getTargetContentNetwork().booleanValue()+"");
|
|
|
127 |
}
|
|
|
128 |
else{
|
|
|
129 |
campaignMap.put("TargetContentNetwork","false");
|
|
|
130 |
}
|
|
|
131 |
campaignDataMainList.add(campaignMap);
|
| 9289 |
manish.sha |
132 |
}
|
| 9005 |
manish.sha |
133 |
}
|
|
|
134 |
} else {
|
|
|
135 |
System.out.println("No campaigns were found.");
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
offset += PAGE_SIZE;
|
|
|
139 |
selector.getPaging().setStartIndex(offset);
|
|
|
140 |
} while (offset < page.getTotalNumEntries());
|
|
|
141 |
return null;
|
|
|
142 |
}
|
|
|
143 |
}
|