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.AdGroupAd;
19
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupAdOperation;
20
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupAdReturnValue;
21
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupAdServiceInterface;
22
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupAdStatus;
23
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
24
import com.google.api.ads.adwords.axis.v201309.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 Long runExample(long adGroupId, String headLine, String description1, String description2, String url, String displayUrl) 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
		AdWordsServices adWordsServices = new AdWordsServices();
61
		// Get the AdGroupAdService.
62
		AdGroupAdServiceInterface adGroupAdService =
63
			adWordsServices.get(session, AdGroupAdServiceInterface.class);
64
 
65
		// Create text ad.
66
		TextAd textAd1 = new TextAd();
67
		textAd1.setHeadline(headLine);
68
		textAd1.setDescription1(description1);
69
		textAd1.setDescription2(description2);
70
		textAd1.setDisplayUrl(displayUrl);
71
		textAd1.setUrl(url);
72
		/*TextAd textAd2 = new TextAd();
73
		textAd2.setHeadline("Luxury Cruise to Mars");
74
		textAd2.setDescription1("Enjoy your stay at Red Planet.");
75
		textAd2.setDescription2("Buy your tickets now!");
76
		textAd2.setDisplayUrl("www.example.com");
77
		textAd2.setUrl("http://www.example.com");*/
78
 
79
 
80
		// Create ad group ad.
81
		AdGroupAd textAdGroupAd1 = new AdGroupAd();
82
		textAdGroupAd1.setAdGroupId(adGroupId);
83
		textAdGroupAd1.setAd(textAd1);
84
 
85
 
86
		// You can optionally provide these field(s).
87
		//textAdGroupAd1.setStatus(AdGroupAdStatus.PAUSED);
88
 
89
		/*AdGroupAd textAdGroupAd2 = new AdGroupAd();
90
		textAdGroupAd2.setAdGroupId(adGroupId);
91
		textAdGroupAd2.setAd(textAd2);*/
92
 
93
 
94
		// Create operations.
95
		AdGroupAdOperation textAdGroupAdOperation1 = new AdGroupAdOperation();
96
		textAdGroupAdOperation1.setOperand(textAdGroupAd1);
97
		textAdGroupAdOperation1.setOperator(Operator.ADD);
98
		/*AdGroupAdOperation textAdGroupAdOperation2 = new AdGroupAdOperation();
99
		textAdGroupAdOperation2.setOperand(textAdGroupAd2);
100
		textAdGroupAdOperation2.setOperator(Operator.ADD);*/
101
 
102
		AdGroupAdOperation[] operations =
103
			new AdGroupAdOperation[] {textAdGroupAdOperation1};
104
 
105
		// Add ads.
106
		AdGroupAdReturnValue result = adGroupAdService.mutate(operations);
107
		Long adgroupAdId = 0l;
108
		// Display ads.
109
		for (AdGroupAd adGroupAdResult : result.getValue()) {
110
			System.out.println("Ad with id  \"" + adGroupAdResult.getAd().getId() + "\"" + " and type \""
111
					+ adGroupAdResult.getAd().getAdType() + "\" was added.");
112
			adgroupAdId = adGroupAdResult.getAd().getId();
113
		}
114
		return adgroupAdId;
115
	}
116
}