Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9005 manish.sha 1
// Copyright 2013 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.adwordsforvideo;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201302.cm.Paging;
19
import com.google.api.ads.adwords.axis.v201302.video.BaseCriterionType;
20
import com.google.api.ads.adwords.axis.v201302.video.BaseKeyword;
21
import com.google.api.ads.adwords.axis.v201302.video.NegativeTargetingGroupCriterion;
22
import com.google.api.ads.adwords.axis.v201302.video.TargetingGroupCriterion;
23
import com.google.api.ads.adwords.axis.v201302.video.TargetingGroupCriterionPage;
24
import com.google.api.ads.adwords.axis.v201302.video.TargetingGroupCriterionSelector;
25
import com.google.api.ads.adwords.axis.v201302.video.VideoTargetingGroupCriterionServiceInterface;
26
import com.google.api.ads.adwords.lib.client.AdWordsSession;
27
import com.google.api.ads.common.lib.auth.OfflineCredentials;
28
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
29
import com.google.api.client.auth.oauth2.Credential;
30
 
31
/**
32
 * This example shows how to retrieve all keywords for a video campaign. To get
33
 * a list with all campaigns, run GetVideoCampaigns.java.
34
 *
35
 * Credentials and properties in {@code fromFile()} are pulled from the
36
 * "ads.properties" file. See README for more info.
37
 *
38
 * Tags: VideoTargetingGroupCriterionService.get
39
 *
40
 * Category: adx-exclude
41
 *
42
 * @author Kevin Winter
43
 */
44
public class GetKeywordCriteria {
45
 
46
  private static final int PAGE_SIZE = 100;
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 VideoTargetingGroupCriterionService.
73
    VideoTargetingGroupCriterionServiceInterface videoTargetingGroupCriterionService =
74
        adWordsServices.get(session, VideoTargetingGroupCriterionServiceInterface.class);
75
 
76
    int offset = 0;
77
    boolean morePages = true;
78
 
79
    // Create selector.
80
    TargetingGroupCriterionSelector selector = new TargetingGroupCriterionSelector();
81
    selector.setCampaignIds(new long[] {campaignId});
82
    selector.setCriterionTypes(new BaseCriterionType[] {BaseCriterionType.SEARCH_KEYWORD});
83
    selector.setPaging(new Paging(offset, PAGE_SIZE));
84
 
85
    while (morePages) {
86
      // Get all keywords.
87
      TargetingGroupCriterionPage page = videoTargetingGroupCriterionService.get(selector);
88
 
89
      // Display keywords.
90
      if (page.getEntries() != null) {
91
        for (TargetingGroupCriterion targetingGroupCriterion : page.getEntries()) {
92
          String negative =
93
              (targetingGroupCriterion instanceof NegativeTargetingGroupCriterion) ? " (negative)"
94
                  : "";
95
          BaseKeyword keyword = (BaseKeyword) targetingGroupCriterion.getCriterion();
96
          System.out.printf("Criterion%s ID %d, targeting group ID %d and text '%s'\n", negative,
97
              keyword.getId(), targetingGroupCriterion.getTargetingGroupId(), keyword.getText());
98
        }
99
      } else {
100
        System.out.println("No keywords were found.");
101
      }
102
 
103
      offset += PAGE_SIZE;
104
      selector.getPaging().setStartIndex(offset);
105
      morePages = offset < page.getTotalNumEntries();
106
    }
107
  }
108
}