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.AdGroupAd;
19
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupAdOperation;
20
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupAdReturnValue;
21
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupAdServiceInterface;
22
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupAdStatus;
23
import com.google.api.ads.adwords.axis.v201302.cm.Operator;
24
import com.google.api.ads.adwords.axis.v201302.cm.TextAd;
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 adds several text ads to a given ad group. To get adGroupId, run
32
 * AddAdGroup.java.
33
 *
34
 * Credentials and properties in {@code fromFile()} are pulled from the
35
 * "ads.properties" file. See README for more info.
36
 *
37
 * Tags: AdGroupAdService.mutate
38
 *
39
 * Category: adx-exclude
40
 *
41
 * @author Kevin Winter
42
 */
43
public class AddTextAds {
44
 
45
  public static void main(String[] args) throws Exception {
46
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
47
    // and can be used in place of a service account.
48
    Credential oAuth2Credential = new OfflineCredentials.Builder()
49
        .forApi(Api.ADWORDS)
50
        .fromFile()
51
        .build()
52
        .generateCredential();
53
 
54
    // Construct an AdWordsSession.
55
    AdWordsSession session = new AdWordsSession.Builder()
56
        .fromFile()
57
        .withOAuth2Credential(oAuth2Credential)
58
        .build();
59
 
60
    long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
61
 
62
    AdWordsServices adWordsServices = new AdWordsServices();
63
 
64
    runExample(adWordsServices, session, adGroupId);
65
  }
66
 
67
  public static void runExample(
68
      AdWordsServices adWordsServices, AdWordsSession session, long adGroupId) throws Exception {
69
    // Get the AdGroupAdService.
70
    AdGroupAdServiceInterface adGroupAdService =
71
        adWordsServices.get(session, AdGroupAdServiceInterface.class);
72
 
73
    // Create text ad.
74
    TextAd textAd1 = new TextAd();
75
    textAd1.setHeadline("Luxury Cruise to Mars");
76
    textAd1.setDescription1("Visit the Red Planet in style.");
77
    textAd1.setDescription2("Low-gravity fun for everyone!");
78
    textAd1.setDisplayUrl("www.example.com");
79
    textAd1.setUrl("http://www.example.com");
80
    TextAd textAd2 = new TextAd();
81
    textAd2.setHeadline("Luxury Cruise to Mars");
82
    textAd2.setDescription1("Enjoy your stay at Red Planet.");
83
    textAd2.setDescription2("Buy your tickets now!");
84
    textAd2.setDisplayUrl("www.example.com");
85
    textAd2.setUrl("http://www.example.com");
86
 
87
 
88
    // Create ad group ad.
89
    AdGroupAd textAdGroupAd1 = new AdGroupAd();
90
    textAdGroupAd1.setAdGroupId(adGroupId);
91
    textAdGroupAd1.setAd(textAd1);
92
 
93
    // You can optionally provide these field(s).
94
    textAdGroupAd1.setStatus(AdGroupAdStatus.PAUSED);
95
 
96
    AdGroupAd textAdGroupAd2 = new AdGroupAd();
97
    textAdGroupAd2.setAdGroupId(adGroupId);
98
    textAdGroupAd2.setAd(textAd2);
99
 
100
 
101
    // Create operations.
102
    AdGroupAdOperation textAdGroupAdOperation1 = new AdGroupAdOperation();
103
    textAdGroupAdOperation1.setOperand(textAdGroupAd1);
104
    textAdGroupAdOperation1.setOperator(Operator.ADD);
105
    AdGroupAdOperation textAdGroupAdOperation2 = new AdGroupAdOperation();
106
    textAdGroupAdOperation2.setOperand(textAdGroupAd2);
107
    textAdGroupAdOperation2.setOperator(Operator.ADD);
108
 
109
    AdGroupAdOperation[] operations =
110
        new AdGroupAdOperation[] {textAdGroupAdOperation1, textAdGroupAdOperation2};
111
 
112
    // Add ads.
113
    AdGroupAdReturnValue result = adGroupAdService.mutate(operations);
114
 
115
    // Display ads.
116
    for (AdGroupAd adGroupAdResult : result.getValue()) {
117
      System.out.println("Ad with id  \"" + adGroupAdResult.getAd().getId() + "\"" + " and type \""
118
          + adGroupAdResult.getAd().getAdType() + "\" was added.");
119
    }
120
  }
121
}