Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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.basicoperations;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201302.cm.AdGroup;
19
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupPage;
20
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupServiceInterface;
21
import com.google.api.ads.adwords.axis.v201302.cm.OrderBy;
22
import com.google.api.ads.adwords.axis.v201302.cm.Paging;
23
import com.google.api.ads.adwords.axis.v201302.cm.Predicate;
24
import com.google.api.ads.adwords.axis.v201302.cm.PredicateOperator;
25
import com.google.api.ads.adwords.axis.v201302.cm.Selector;
26
import com.google.api.ads.adwords.axis.v201302.cm.SortOrder;
27
import com.google.api.ads.adwords.lib.client.AdWordsSession;
28
import com.google.api.ads.common.lib.auth.OfflineCredentials;
29
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
30
import com.google.api.client.auth.oauth2.Credential;
31
 
32
/**
33
 * This example gets all ad groups in a campaign. To add an ad group, run
34
 * AddAdGroup.java. To get campaigns, run GetCampaigns.java.
35
 *
36
 * Credentials and properties in {@code fromFile()} are pulled from the
37
 * "ads.properties" file. See README for more info.
38
 *
39
 * Tags: AdGroupService.get
40
 *
41
 * @author Kevin Winter
42
 */
43
public class GetAdGroups {
44
 
45
  private static final int PAGE_SIZE = 100;
46
 
47
  public static void main(String[] args) throws Exception {
48
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
49
    // and can be used in place of a service account.
50
    Credential oAuth2Credential = new OfflineCredentials.Builder()
51
        .forApi(Api.ADWORDS)
52
        .fromFile()
53
        .build()
54
        .generateCredential();
55
 
56
    // Construct an AdWordsSession.
57
    AdWordsSession session = new AdWordsSession.Builder()
58
        .fromFile()
59
        .withOAuth2Credential(oAuth2Credential)
60
        .build();
61
 
62
    Long campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE");
63
 
64
    AdWordsServices adWordsServices = new AdWordsServices();
65
 
66
    runExample(adWordsServices, session, campaignId);
67
  }
68
 
69
  public static void runExample(
70
      AdWordsServices adWordsServices, AdWordsSession session, Long campaignId) throws Exception {
71
    // Get the AdGroupService.
72
    AdGroupServiceInterface adGroupService =
73
        adWordsServices.get(session, AdGroupServiceInterface.class);
74
 
75
    int offset = 0;
76
    boolean morePages = true;
77
 
78
    // Create selector.
79
    Selector selector = new Selector();
80
    selector.setFields(new String[] {"Id", "Name"});
81
    selector.setOrdering(new OrderBy[] {new OrderBy("Name", SortOrder.ASCENDING)});
82
    selector.setPaging(new Paging(offset, PAGE_SIZE));
83
 
84
    // Create predicates.
85
    Predicate campaignIdPredicate =
86
        new Predicate("CampaignId", PredicateOperator.IN, new String[] {campaignId.toString()});
87
    selector.setPredicates(new Predicate[] {campaignIdPredicate});
88
 
89
    while (morePages) {
90
      // Get all ad groups.
91
      AdGroupPage page = adGroupService.get(selector);
92
 
93
      // Display ad groups.
94
      if (page.getEntries() != null) {
95
        for (AdGroup adGroup : page.getEntries()) {
96
          System.out.println("Ad group with name \"" + adGroup.getName() + "\" and id \""
97
              + adGroup.getId() + "\" was found.");
98
        }
99
      } else {
100
        System.out.println("No ad groups were found.");
101
      }
102
 
103
      offset += PAGE_SIZE;
104
      selector.getPaging().setStartIndex(offset);
105
      morePages = offset < page.getTotalNumEntries();
106
    }
107
  }
108
}