| 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.misc;
|
|
|
16 |
|
|
|
17 |
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
|
|
|
18 |
import com.google.api.ads.adwords.axis.v201306.cm.Dimensions;
|
|
|
19 |
import com.google.api.ads.adwords.axis.v201306.cm.Image;
|
|
|
20 |
import com.google.api.ads.adwords.axis.v201306.cm.Media;
|
|
|
21 |
import com.google.api.ads.adwords.axis.v201306.cm.MediaMediaType;
|
|
|
22 |
import com.google.api.ads.adwords.axis.v201306.cm.MediaServiceInterface;
|
|
|
23 |
import com.google.api.ads.adwords.axis.v201306.cm.MediaSize;
|
|
|
24 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
25 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
26 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
27 |
import com.google.api.ads.common.lib.utils.Maps;
|
|
|
28 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
29 |
|
|
|
30 |
import java.util.Map;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* This example uploads an image. To get images, run GetAllImagesAndVideos.java.
|
|
|
34 |
*
|
|
|
35 |
* Credentials and properties in {@code fromFile()} are pulled from the
|
|
|
36 |
* "ads.properties" file. See README for more info.
|
|
|
37 |
*
|
|
|
38 |
* Tags: MediaService.upload
|
|
|
39 |
*
|
|
|
40 |
* @author Kevin Winter
|
|
|
41 |
*/
|
|
|
42 |
public class UploadImage {
|
|
|
43 |
|
|
|
44 |
public static void main(String[] args) throws Exception {
|
|
|
45 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
46 |
// and can be used in place of a service account.
|
|
|
47 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
48 |
.forApi(Api.ADWORDS)
|
|
|
49 |
.fromFile()
|
|
|
50 |
.build()
|
|
|
51 |
.generateCredential();
|
|
|
52 |
|
|
|
53 |
// Construct an AdWordsSession.
|
|
|
54 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
55 |
.fromFile()
|
|
|
56 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
57 |
.build();
|
|
|
58 |
|
|
|
59 |
AdWordsServices adWordsServices = new AdWordsServices();
|
|
|
60 |
|
|
|
61 |
runExample(adWordsServices, session);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public static void runExample(
|
|
|
65 |
AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
|
|
|
66 |
// Get the MediaService.
|
|
|
67 |
MediaServiceInterface mediaService =
|
|
|
68 |
adWordsServices.get(session, MediaServiceInterface.class);
|
|
|
69 |
|
|
|
70 |
// Create image.
|
|
|
71 |
Image image = new Image();
|
|
|
72 |
image.setData(
|
|
|
73 |
com.google.api.ads.common.lib.utils.Media.getMediaDataFromUrl("http://goo.gl/HJM3L"));
|
|
|
74 |
image.setType(MediaMediaType.IMAGE);
|
|
|
75 |
|
|
|
76 |
Media[] media = new Media[] {image};
|
|
|
77 |
|
|
|
78 |
// Upload image.
|
|
|
79 |
Media[] result = mediaService.upload(media);
|
|
|
80 |
|
|
|
81 |
// Display images.
|
|
|
82 |
image = (Image) result[0];
|
|
|
83 |
Map<MediaSize, Dimensions> dimensions = Maps.toMap(image.getDimensions());
|
|
|
84 |
System.out.println("Image with id '" + image.getMediaId() + "', dimensions '"
|
|
|
85 |
+ dimensions.get(MediaSize.FULL).getWidth() + "x"
|
|
|
86 |
+ dimensions.get(MediaSize.FULL).getHeight() + "', and MIME type '" + image.getMediaType()
|
|
|
87 |
+ "' was uploaded.");
|
|
|
88 |
}
|
|
|
89 |
}
|