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.v201306.campaignmanagement;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupAd;
19
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupAdOperation;
20
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupAdReturnValue;
21
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupAdServiceInterface;
22
import com.google.api.ads.adwords.axis.v201306.cm.ApiException;
23
import com.google.api.ads.adwords.axis.v201306.cm.Operator;
24
import com.google.api.ads.adwords.axis.v201306.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 shows how to use the validate only header through the
32
 * {@link AdWordsSession#setValidateOnly(Boolean)} method. No objects will be
33
 * created, but exceptions will still be thrown.
34
 *
35
 * Credentials and properties in {@code fromFile()} are pulled from the
36
 * "ads.properties" file. See README for more info.
37
 *
38
 * Tags: AdGroupAdService.mutate
39
 *
40
 * Category: adx-exclude
41
 *
42
 * @author Kevin Winter
43
 */
44
public class ValidateTextAd {
45
 
46
  public static void main(String[] args) throws Exception {
47
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
48
    // and can be used in place of a service account.
49
    Credential oAuth2Credential = new OfflineCredentials.Builder()
50
        .forApi(Api.ADWORDS)
51
        .fromFile()
52
        .build()
53
        .generateCredential();
54
 
55
    // Construct an AdWordsSession.
56
    AdWordsSession session = new AdWordsSession.Builder()
57
        .fromFile()
58
        .withOAuth2Credential(oAuth2Credential)
59
        .build();
60
 
61
    long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
62
 
63
    AdWordsServices adWordsServices = new AdWordsServices();
64
 
65
    runExample(adWordsServices, session, adGroupId);
66
  }
67
 
68
  public static void runExample(
69
      AdWordsServices adWordsServices, AdWordsSession session, Long adGroupId) throws Exception {
70
    // Enable validation.
71
    session.setValidateOnly(true);
72
 
73
    // Get the validation AdGroupAdService.
74
    AdGroupAdServiceInterface adGroupAdValidationService =
75
        adWordsServices.get(session, AdGroupAdServiceInterface.class);
76
 
77
    // Create text ad.
78
    TextAd textAd1 = new TextAd();
79
    textAd1.setHeadline("Luxury Cruise to Mars");
80
    textAd1.setDescription1("Visit the Red Planet in style.");
81
    textAd1.setDescription2("Low-gravity fun for everyone!");
82
    textAd1.setDisplayUrl("www.example.com");
83
    textAd1.setUrl("http://www.example.com");
84
 
85
    // Create ad group ad.
86
    AdGroupAd textAdGroupAd1 = new AdGroupAd();
87
    textAdGroupAd1.setAdGroupId(adGroupId);
88
    textAdGroupAd1.setAd(textAd1);
89
 
90
    // Create operations.
91
    AdGroupAdOperation textAdGroupAdOperation1 = new AdGroupAdOperation();
92
    textAdGroupAdOperation1.setOperand(textAdGroupAd1);
93
    textAdGroupAdOperation1.setOperator(Operator.ADD);
94
 
95
    AdGroupAdOperation[] operations = new AdGroupAdOperation[] {textAdGroupAdOperation1};
96
 
97
    // Add ads.
98
    AdGroupAdReturnValue result = adGroupAdValidationService.mutate(operations);
99
    // No error means the request is valid.
100
 
101
    // Now let's check an invalid ad using a very long line to trigger an error.
102
    textAd1.setDescription2("Low-gravity fun for all astronauts in orbit.");
103
 
104
    try {
105
      adGroupAdValidationService.mutate(operations);
106
    } catch (ApiException e) {
107
      System.err.println("Validation failed for reason \"" + e.getMessage1() + "\".");
108
    }
109
  }
110
}