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.Campaign;
19
import com.google.api.ads.adwords.axis.v201302.cm.CampaignPage;
20
import com.google.api.ads.adwords.axis.v201302.cm.CampaignServiceInterface;
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.Selector;
24
import com.google.api.ads.adwords.axis.v201302.cm.SortOrder;
25
import com.google.api.ads.adwords.lib.client.AdWordsSession;
26
import com.google.api.ads.common.lib.auth.OfflineCredentials;
27
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
28
import com.google.api.client.auth.oauth2.Credential;
29
 
30
/**
31
 * This example gets all campaigns. To add a campaign, run AddCampaign.java.
32
 *
33
 * Credentials and properties in {@code fromFile()} are pulled from the
34
 * "ads.properties" file. See README for more info.
35
 *
36
 * Tags: CampaignService.get
37
 *
38
 * @author Adam Rogal
39
 */
40
public class GetCampaigns {
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
    AdWordsServices adWordsServices = new AdWordsServices();
60
 
61
    runExample(adWordsServices, session);
62
  }
63
 
64
  public static void runExample(
65
      AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
66
    // Get the CampaignService.
67
    CampaignServiceInterface campaignService =
68
        adWordsServices.get(session, CampaignServiceInterface.class);
69
 
70
    int offset = 0;
71
 
72
    // Create selector.
73
    Selector selector = new Selector();
74
    selector.setFields(new String[] {"Id", "Name"});
75
    selector.setOrdering(new OrderBy[] {new OrderBy("Name", SortOrder.ASCENDING)});
76
    selector.setPaging(new Paging(offset, PAGE_SIZE));
77
 
78
    CampaignPage page = null;
79
    do {
80
      // Get all campaigns.
81
      page = campaignService.get(selector);
82
 
83
      // Display campaigns.
84
      if (page.getEntries() != null) {
85
        for (Campaign campaign : page.getEntries()) {
86
          System.out.println("Campaign with name \"" + campaign.getName() + "\" and id \""
87
              + campaign.getId() + "\" was found.");
88
        }
89
      } else {
90
        System.out.println("No campaigns were found.");
91
      }
92
 
93
      offset += PAGE_SIZE;
94
      selector.getPaging().setStartIndex(offset);
95
    } while (offset < page.getTotalNumEntries());
96
  }
97
}