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.campaignmanagement;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201309.cm.AdParam;
19
import com.google.api.ads.adwords.axis.v201309.cm.AdParamOperation;
20
import com.google.api.ads.adwords.axis.v201309.cm.AdParamServiceInterface;
21
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
22
import com.google.api.ads.adwords.lib.client.AdWordsSession;
23
import com.google.api.ads.common.lib.auth.OfflineCredentials;
24
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
25
import com.google.api.client.auth.oauth2.Credential;
26
 
27
/**
28
 * This example sets ad parameters for a keyword ad group criterion. To get ad
29
 * group criteria, run GetKeywords.java.
30
 *
31
 * Credentials and properties in {@code fromFile()} are pulled from the
32
 * "ads.properties" file. See README for more info.
33
 *
34
 * Tags: AdParamService.mutate
35
 *
36
 * Category: adx-exclude
37
 *
38
 * @author Kevin Winter
39
 */
40
public class SetAdParameters {
41
 
42
  public static void main(String[] args) throws Exception {
43
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
44
    // and can be used in place of a service account.
45
    Credential oAuth2Credential = new OfflineCredentials.Builder()
46
        .forApi(Api.ADWORDS)
47
        .fromFile()
48
        .build()
49
        .generateCredential();
50
 
51
    // Construct an AdWordsSession.
52
    AdWordsSession session = new AdWordsSession.Builder()
53
        .fromFile()
54
        .withOAuth2Credential(oAuth2Credential)
55
        .build();
56
 
57
    long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
58
    long keywordId = Long.parseLong("INSERT_KEYWORD_ID_HERE");
59
 
60
    AdWordsServices adWordsServices = new AdWordsServices();
61
 
62
    runExample(adWordsServices, session, adGroupId, keywordId);
63
  }
64
 
65
  public static void runExample(
66
      AdWordsServices adWordsServices, AdWordsSession session, Long adGroupId, Long keywordId)
67
      throws Exception {
68
    // Get the AdParamService.
69
    AdParamServiceInterface adParamService =
70
        adWordsServices.get(session, AdParamServiceInterface.class);
71
 
72
    // Create ad params.
73
    AdParam adParam1 = new AdParam();
74
    adParam1.setAdGroupId(adGroupId);
75
    adParam1.setCriterionId(keywordId);
76
    adParam1.setInsertionText("100");
77
    adParam1.setParamIndex(1);
78
 
79
    AdParam adParam2 = new AdParam();
80
    adParam2.setAdGroupId(adGroupId);
81
    adParam2.setCriterionId(keywordId);
82
    adParam2.setInsertionText("$40");
83
    adParam2.setParamIndex(2);
84
 
85
    // Create operations.
86
    AdParamOperation adParamOperation1 = new AdParamOperation();
87
    adParamOperation1.setOperand(adParam1);
88
    adParamOperation1.setOperator(Operator.SET);
89
 
90
    AdParamOperation adParamOperation2 = new AdParamOperation();
91
    adParamOperation2.setOperand(adParam2);
92
    adParamOperation2.setOperator(Operator.SET);
93
 
94
    AdParamOperation[] operations = new AdParamOperation[] {adParamOperation1, adParamOperation2};
95
 
96
    // Set ad parameters.
97
    AdParam[] adParams = adParamService.mutate(operations);
98
 
99
    // Display ad parameters.
100
    for (AdParam adParam : adParams) {
101
      System.out.println("Ad parameter with ad group id \"" + adParam.getAdGroupId()
102
          + "\", criterion id \"" + adParam.getCriterionId() + "\", insertion text \""
103
          + adParam.getInsertionText() + "\", and parameter index \"" + adParam.getParamIndex()
104
          + "\" was set.");
105
    }
106
  }
107
}