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.AdGroupAd;
19
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupAdPage;
20
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupAdServiceInterface;
21
import com.google.api.ads.adwords.axis.v201306.cm.OrderBy;
22
import com.google.api.ads.adwords.axis.v201306.cm.Paging;
23
import com.google.api.ads.adwords.axis.v201306.cm.Predicate;
24
import com.google.api.ads.adwords.axis.v201306.cm.PredicateOperator;
25
import com.google.api.ads.adwords.axis.v201306.cm.Selector;
26
import com.google.api.ads.adwords.axis.v201306.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 text ads for a given ad group. To add an ad, run
34
 * AddTextAds.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: AdGroupAdService.get
40
 *
41
 * Category: adx-exclude
42
 *
43
 * @author Kevin Winter
44
 */
45
public class GetTextAds {
46
 
47
  private static final int PAGE_SIZE = 100;
48
 
49
  public static void main(String[] args) throws Exception {
50
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
51
    // and can be used in place of a service account.
52
    Credential oAuth2Credential = new OfflineCredentials.Builder()
53
        .forApi(Api.ADWORDS)
54
        .fromFile()
55
        .build()
56
        .generateCredential();
57
 
58
    // Construct an AdWordsSession.
59
    AdWordsSession session = new AdWordsSession.Builder()
60
        .fromFile()
61
        .withOAuth2Credential(oAuth2Credential)
62
        .build();
63
 
64
    Long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
65
 
66
    AdWordsServices adWordsServices = new AdWordsServices();
67
 
68
    runExample(adWordsServices, session, adGroupId);
69
  }
70
 
71
  public static void runExample(
72
      AdWordsServices adWordsServices, AdWordsSession session, Long adGroupId) throws Exception {
73
    // Get the AdGroupAdService.
74
    AdGroupAdServiceInterface adGroupAdService =
75
        adWordsServices.get(session, AdGroupAdServiceInterface.class);
76
 
77
    int offset = 0;
78
    boolean morePages = true;
79
 
80
    // Create selector.
81
    Selector selector = new Selector();
82
    selector.setFields(new String[] {"Id", "AdGroupId", "Status"});
83
    selector.setOrdering(new OrderBy[] {new OrderBy("Id", SortOrder.ASCENDING)});
84
    selector.setPaging(new Paging(offset, PAGE_SIZE));
85
 
86
    // Create predicates.
87
    Predicate adGroupIdPredicate =
88
        new Predicate("AdGroupId", PredicateOperator.IN, new String[] {adGroupId.toString()});
89
    // By default disabled ads aren't returned by the selector. To return them
90
    // include the DISABLED status in a predicate.
91
    Predicate statusPredicate =
92
        new Predicate("Status", PredicateOperator.IN,
93
            new String[] {"ENABLED", "PAUSED", "DISABLED"});
94
    Predicate adTypePredicate =
95
        new Predicate("AdType", PredicateOperator.EQUALS, new String[] {"TEXT_AD"});
96
    selector.setPredicates(new Predicate[] {adGroupIdPredicate, statusPredicate, adTypePredicate});
97
 
98
    while (morePages) {
99
      // Get all ads.
100
      AdGroupAdPage page = adGroupAdService.get(selector);
101
 
102
      // Display ads.
103
      if (page.getEntries() != null && page.getEntries().length > 0) {
104
        for (AdGroupAd adGroupAd : page.getEntries()) {
105
          System.out.println("Ad with id  \"" + adGroupAd.getAd().getId() + "\"" + " and type \""
106
              + adGroupAd.getAd().getAdType() + "\" was found.");
107
        }
108
      } else {
109
        System.out.println("No ads were found.");
110
      }
111
 
112
      offset += PAGE_SIZE;
113
      selector.getPaging().setStartIndex(offset);
114
      morePages = offset < page.getTotalNumEntries();
115
    }
116
  }
117
}