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.TargetingGroup;
20
import com.google.api.ads.adwords.axis.v201302.video.TargetingGroupPage;
21
import com.google.api.ads.adwords.axis.v201302.video.TargetingGroupSelector;
22
import com.google.api.ads.adwords.axis.v201302.video.VideoTargetingGroupServiceInterface;
23
import com.google.api.ads.adwords.lib.client.AdWordsSession;
24
import com.google.api.ads.common.lib.auth.OfflineCredentials;
25
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
26
import com.google.api.client.auth.oauth2.Credential;
27
 
28
/**
29
 * This example shows how to retrieve all targeting groups for video campaigns.
30
 *
31
 * Credentials and properties in {@code fromFile()} are pulled from the
32
 * "ads.properties" file. See README for more info.
33
 *
34
 * Tags: VideoTargetingGroupService.get
35
 *
36
 * Category: adx-exclude
37
 *
38
 * @author Kevin Winter
39
 */
40
public class GetTargetingGroups {
41
 
42
  private static final int PAGE_SIZE = 100;
43
 
44
  public static void main(String[] args) throws Exception {
45
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
46
    // and can be used in place of a service account.
47
    Credential oAuth2Credential = new OfflineCredentials.Builder()
48
        .forApi(Api.ADWORDS)
49
        .fromFile()
50
        .build()
51
        .generateCredential();
52
 
53
    // Construct an AdWordsSession.
54
    AdWordsSession session = new AdWordsSession.Builder()
55
        .fromFile()
56
        .withOAuth2Credential(oAuth2Credential)
57
        .build();
58
 
59
    Long campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE");
60
 
61
    AdWordsServices adWordsServices = new AdWordsServices();
62
 
63
    runExample(adWordsServices, session, campaignId);
64
  }
65
 
66
  public static void runExample(
67
      AdWordsServices adWordsServices, AdWordsSession session, Long campaignId) throws Exception {
68
    // Get the VideoTargetingGroupService.
69
    VideoTargetingGroupServiceInterface videoTargetingGroupService =
70
        adWordsServices.get(session, VideoTargetingGroupServiceInterface.class);
71
 
72
    int offset = 0;
73
    boolean morePages = true;
74
 
75
    // Create selector.
76
    TargetingGroupSelector selector = new TargetingGroupSelector();
77
    selector.setCampaignIds(new long[] {campaignId});
78
    selector.setPaging(new Paging(offset, PAGE_SIZE));
79
 
80
    while (morePages) {
81
      // Get all targeting groups for this account.
82
      TargetingGroupPage page = videoTargetingGroupService.get(selector);
83
 
84
      // Display targeting groups.
85
      if (page.getEntries() != null) {
86
        for (TargetingGroup targetingGroup : page.getEntries()) {
87
          System.out.printf("Targeting group ID %d, campaign ID %d and name '%s'\n",
88
              targetingGroup.getId(), targetingGroup.getCampaignId(), targetingGroup.getName());
89
        }
90
      } else {
91
        System.out.println("No targeting groups were found.");
92
      }
93
 
94
      offset += PAGE_SIZE;
95
      selector.getPaging().setStartIndex(offset);
96
      morePages = offset < page.getTotalNumEntries();
97
    }
98
  }
99
}