| 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.errorhandling;
|
|
|
16 |
|
|
|
17 |
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
|
|
|
18 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterion;
|
|
|
19 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterionOperation;
|
|
|
20 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterionReturnValue;
|
|
|
21 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterionServiceInterface;
|
|
|
22 |
import com.google.api.ads.adwords.axis.v201309.cm.ApiError;
|
|
|
23 |
import com.google.api.ads.adwords.axis.v201309.cm.BiddableAdGroupCriterion;
|
|
|
24 |
import com.google.api.ads.adwords.axis.v201309.cm.Keyword;
|
|
|
25 |
import com.google.api.ads.adwords.axis.v201309.cm.KeywordMatchType;
|
|
|
26 |
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
|
|
|
27 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
28 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
29 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
30 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
31 |
|
|
|
32 |
import java.util.ArrayList;
|
|
|
33 |
import java.util.List;
|
|
|
34 |
import java.util.regex.Matcher;
|
|
|
35 |
import java.util.regex.Pattern;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* This example demonstrates how to handle partial failures.
|
|
|
39 |
*
|
|
|
40 |
* Credentials and properties in {@code fromFile()} are pulled from the
|
|
|
41 |
* "ads.properties" file. See README for more info.
|
|
|
42 |
*
|
|
|
43 |
* Tags: AdGroupCriterionService.mutate
|
|
|
44 |
*
|
|
|
45 |
* Category: adx-exclude
|
|
|
46 |
*
|
|
|
47 |
* @author Adam Rogal
|
|
|
48 |
*/
|
|
|
49 |
public class HandlePartialFailures {
|
|
|
50 |
|
|
|
51 |
private static Pattern operationIndexPattern = Pattern.compile("^.*operations\\[(\\d+)\\].*$");
|
|
|
52 |
|
|
|
53 |
public static void main(String[] args) throws Exception {
|
|
|
54 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
55 |
// and can be used in place of a service account.
|
|
|
56 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
57 |
.forApi(Api.ADWORDS)
|
|
|
58 |
.fromFile()
|
|
|
59 |
.build()
|
|
|
60 |
.generateCredential();
|
|
|
61 |
|
|
|
62 |
// Construct an AdWordsSession.
|
|
|
63 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
64 |
.fromFile()
|
|
|
65 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
66 |
.build();
|
|
|
67 |
|
|
|
68 |
long adGroupId = Long.parseLong("INSERT_ADGROUP_ID_HERE");
|
|
|
69 |
AdWordsServices adWordsServices = new AdWordsServices();
|
|
|
70 |
|
|
|
71 |
runExample(adWordsServices, session, adGroupId);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public static void runExample(
|
|
|
75 |
AdWordsServices adWordsServices, AdWordsSession session, Long adGroupId) throws Exception {
|
|
|
76 |
// Enable partial failure.
|
|
|
77 |
session.setPartialFailure(true);
|
|
|
78 |
|
|
|
79 |
// Get the AdGroupCriterionService.
|
|
|
80 |
AdGroupCriterionServiceInterface adGroupCriterionService =
|
|
|
81 |
adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
|
|
|
82 |
|
|
|
83 |
List<AdGroupCriterionOperation> operations = new ArrayList<AdGroupCriterionOperation>();
|
|
|
84 |
|
|
|
85 |
// Create keywords.
|
|
|
86 |
String[] keywords =
|
|
|
87 |
new String[] {"mars cruise", "inv@lid cruise", "venus cruise", "b(a)d keyword cruise"};
|
|
|
88 |
for (String keywordText : keywords) {
|
|
|
89 |
// Create keyword
|
|
|
90 |
Keyword keyword = new Keyword();
|
|
|
91 |
keyword.setText(keywordText);
|
|
|
92 |
keyword.setMatchType(KeywordMatchType.BROAD);
|
|
|
93 |
|
|
|
94 |
// Create biddable ad group criterion.
|
|
|
95 |
BiddableAdGroupCriterion keywordBiddableAdGroupCriterion = new BiddableAdGroupCriterion();
|
|
|
96 |
keywordBiddableAdGroupCriterion.setAdGroupId(adGroupId);
|
|
|
97 |
keywordBiddableAdGroupCriterion.setCriterion(keyword);
|
|
|
98 |
|
|
|
99 |
// Create operation.
|
|
|
100 |
AdGroupCriterionOperation keywordAdGroupCriterionOperation =
|
|
|
101 |
new AdGroupCriterionOperation();
|
|
|
102 |
keywordAdGroupCriterionOperation.setOperand(keywordBiddableAdGroupCriterion);
|
|
|
103 |
keywordAdGroupCriterionOperation.setOperator(Operator.ADD);
|
|
|
104 |
operations.add(keywordAdGroupCriterionOperation);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// Add ad group criteria.
|
|
|
108 |
AdGroupCriterionReturnValue result =
|
|
|
109 |
adGroupCriterionService.mutate(operations.toArray(new AdGroupCriterionOperation[] {}));
|
|
|
110 |
|
|
|
111 |
// Display results.
|
|
|
112 |
for (AdGroupCriterion adGroupCriterionResult : result.getValue()) {
|
|
|
113 |
if (adGroupCriterionResult.getCriterion() != null) {
|
|
|
114 |
System.out.printf("Ad group criterion with ad group id '%d', and criterion id '%d', "
|
|
|
115 |
+ "and keyword '%s' was added.\n", adGroupCriterionResult.getAdGroupId(),
|
|
|
116 |
adGroupCriterionResult.getCriterion().getId(),
|
|
|
117 |
((Keyword) adGroupCriterionResult.getCriterion()).getText());
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
for (ApiError apiError : result.getPartialFailureErrors()) {
|
|
|
122 |
Matcher matcher = operationIndexPattern.matcher(apiError.getFieldPath());
|
|
|
123 |
if (matcher.matches()) {
|
|
|
124 |
int operationIndex = Integer.parseInt(matcher.group(1));
|
|
|
125 |
AdGroupCriterion adGroupCriterion = operations.get(operationIndex).getOperand();
|
|
|
126 |
System.out.printf("Ad group criterion with ad group id '%d' and keyword '%s' "
|
|
|
127 |
+ "triggered a failure for the following reason: '%s'.\n",
|
|
|
128 |
adGroupCriterion.getAdGroupId(),
|
|
|
129 |
((Keyword) adGroupCriterion.getCriterion()).getText(), apiError.getErrorString());
|
|
|
130 |
} else {
|
|
|
131 |
System.out.printf("A failure for the following reason: '%s' has occurred.\n",
|
|
|
132 |
apiError.getErrorString());
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
}
|