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.v201309.basicoperations;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201309.cm.Campaign;
19
import com.google.api.ads.adwords.axis.v201309.cm.CampaignPage;
20
import com.google.api.ads.adwords.axis.v201309.cm.CampaignServiceInterface;
21
import com.google.api.ads.adwords.lib.client.AdWordsSession;
22
import com.google.api.ads.common.lib.auth.OfflineCredentials;
23
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
24
import com.google.api.client.auth.oauth2.Credential;
25
 
26
/**
27
 * This example gets all campaigns with AWQL. To add a campaign, run
28
 * AddCampaign.java.
29
 *
30
 * Credentials and properties in {@code fromFile()} are pulled from the
31
 * "ads.properties" file. See README for more info.
32
 *
33
 * Tags: CampaignService.get
34
 *
35
 * @author Kevin Winter
36
 */
37
public class GetCampaignsWithAwql {
38
 
39
  private static final int PAGE_SIZE = 100;
40
 
41
  public static void main(String[] args) throws Exception {
42
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
43
    // and can be used in place of a service account.
44
    Credential oAuth2Credential = new OfflineCredentials.Builder()
45
        .forApi(Api.ADWORDS)
46
        .fromFile()
47
        .build()
48
        .generateCredential();
49
 
50
    // Construct an AdWordsSession.
51
    AdWordsSession session = new AdWordsSession.Builder()
52
        .fromFile()
53
        .withOAuth2Credential(oAuth2Credential)
54
        .build();
55
 
56
    AdWordsServices adWordsServices = new AdWordsServices();
57
 
58
    runExample(adWordsServices, session);
59
  }
60
 
61
  public static void runExample(
62
      AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
63
    // Get the CampaignService.
64
    CampaignServiceInterface campaignService =
65
        adWordsServices.get(session, CampaignServiceInterface.class);
66
 
67
    int offset = 0;
68
 
69
    String query = "SELECT Id, Name, Status ORDER BY Name";
70
 
71
 
72
    CampaignPage page = null;
73
    do {
74
      String pageQuery = query + String.format(" LIMIT %d, %d", offset, PAGE_SIZE);
75
      // Get all campaigns.
76
      page = campaignService.query(pageQuery);
77
 
78
      // Display campaigns.
79
      if (page.getEntries() != null) {
80
        for (Campaign campaign : page.getEntries()) {
81
          System.out.println("Campaign with name \"" + campaign.getName() + "\" and id \""
82
              + campaign.getId() + "\" was found.");
83
        }
84
      } else {
85
        System.out.println("No campaigns were found.");
86
      }
87
 
88
      offset += PAGE_SIZE;
89
    } while (offset < page.getTotalNumEntries());
90
  }
91
}