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.v201306.basicoperations;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupCriterion;
19
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupCriterionPage;
20
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupCriterionServiceInterface;
21
import com.google.api.ads.adwords.axis.v201306.cm.Keyword;
22
import com.google.api.ads.adwords.axis.v201306.cm.OrderBy;
23
import com.google.api.ads.adwords.axis.v201306.cm.Paging;
24
import com.google.api.ads.adwords.axis.v201306.cm.Predicate;
25
import com.google.api.ads.adwords.axis.v201306.cm.PredicateOperator;
26
import com.google.api.ads.adwords.axis.v201306.cm.Selector;
27
import com.google.api.ads.adwords.axis.v201306.cm.SortOrder;
28
import com.google.api.ads.adwords.lib.client.AdWordsSession;
29
import com.google.api.ads.common.lib.auth.OfflineCredentials;
30
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
31
import com.google.api.client.auth.oauth2.Credential;
32
 
33
/**
34
 * This example gets all ad group criteria in an account. To add keywords
35
 * criteria, run AddKeywords.java
36
 *
37
 * Credentials and properties in {@code fromFile()} are pulled from the
38
 * "ads.properties" file. See README for more info.
39
 *
40
 * Tags: AdGroupCriterionService.get
41
 *
42
 * Category: adx-exclude
43
 *
44
 * @author Kevin Winter
45
 */
46
public class GetKeywords {
47
 
48
  private static final int PAGE_SIZE = 100;
49
 
50
  public static void main(String[] args) throws Exception {
51
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
52
    // and can be used in place of a service account.
53
    Credential oAuth2Credential = new OfflineCredentials.Builder()
54
        .forApi(Api.ADWORDS)
55
        .fromFile()
56
        .build()
57
        .generateCredential();
58
 
59
    // Construct an AdWordsSession.
60
    AdWordsSession session = new AdWordsSession.Builder()
61
        .fromFile()
62
        .withOAuth2Credential(oAuth2Credential)
63
        .build();
64
 
65
    Long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
66
 
67
    AdWordsServices adWordsServices = new AdWordsServices();
68
 
69
    runExample(adWordsServices, session, adGroupId);
70
  }
71
 
72
  public static void runExample(
73
      AdWordsServices adWordsServices, AdWordsSession session, Long adGroupId) throws Exception {
74
    // Get the AdGroupCriterionService.
75
    AdGroupCriterionServiceInterface adGroupCriterionService =
76
        adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
77
 
78
    int offset = 0;
79
    boolean morePages = true;
80
 
81
    // Create selector.
82
    Selector selector = new Selector();
83
    selector.setFields(new String[] {"Id", "AdGroupId", "MatchType", "KeywordText"});
84
    selector.setOrdering(new OrderBy[] {new OrderBy("AdGroupId", SortOrder.ASCENDING)});
85
    selector.setPaging(new Paging(offset, PAGE_SIZE));
86
 
87
    // Create predicates.
88
    Predicate adGroupIdPredicate =
89
        new Predicate("AdGroupId", PredicateOperator.IN, new String[] {adGroupId.toString()});
90
    Predicate criteriaTypePredicate =
91
        new Predicate("CriteriaType", PredicateOperator.EQUALS, new String[] {"KEYWORD"});
92
    selector.setPredicates(new Predicate[] {adGroupIdPredicate, criteriaTypePredicate});
93
 
94
    while (morePages) {
95
      // Get all ad group criteria.
96
      AdGroupCriterionPage page = adGroupCriterionService.get(selector);
97
 
98
      // Display ad group criteria.
99
      if (page.getEntries() != null && page.getEntries().length > 0) {
100
        // Display results.
101
        for (AdGroupCriterion adGroupCriterionResult : page.getEntries()) {
102
          System.out.println("Keyword ad group criterion with ad group id \""
103
              + adGroupCriterionResult.getAdGroupId() + "\", criterion id \""
104
              + adGroupCriterionResult.getCriterion().getId() + "\", text \""
105
              + ((Keyword) adGroupCriterionResult.getCriterion()).getText()
106
              + "\" and match type \""
107
              + ((Keyword) adGroupCriterionResult.getCriterion()).getMatchType() + "\" was found.");
108
        }
109
      } else {
110
        System.out.println("No ad group criteria were found.");
111
      }
112
 
113
      offset += PAGE_SIZE;
114
      selector.getPaging().setStartIndex(offset);
115
      morePages = offset < page.getTotalNumEntries();
116
    }
117
  }
118
}