Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9005 manish.sha 1
// Copyright 2013 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.v201302.adwordsforvideo;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201302.cm.Paging;
19
import com.google.api.ads.adwords.axis.v201302.video.VideoSearchPage;
20
import com.google.api.ads.adwords.axis.v201302.video.VideoSearchSelector;
21
import com.google.api.ads.adwords.axis.v201302.video.VideoSearchSelectorSearchType;
22
import com.google.api.ads.adwords.axis.v201302.video.VideoServiceInterface;
23
import com.google.api.ads.adwords.axis.v201302.video.YouTubeVideo;
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.client.auth.oauth2.Credential;
28
 
29
/**
30
 * This example illustrates how to find YouTube videos by a search string. It
31
 * retrieve details for the first 100 matching videos.
32
 *
33
 * Credentials and properties in {@code fromFile()} are pulled from the
34
 * "ads.properties" file. See README for more info.
35
 *
36
 * Tags: VideoService.search
37
 *
38
 * Category: adx-exclude
39
 *
40
 * @author Kevin Winter
41
 */
42
public class FindVideos {
43
 
44
  private static final int PAGE_SIZE = 100;
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
    AdWordsServices adWordsServices = new AdWordsServices();
62
 
63
    runExample(adWordsServices, session);
64
  }
65
 
66
  public static void runExample(AdWordsServices adWordsServices, AdWordsSession session)
67
      throws Exception {
68
 
69
    // Get the VideoCampaignService.
70
    VideoServiceInterface videoService = adWordsServices.get(session, VideoServiceInterface.class);
71
 
72
    String queryString = "AdWords API with Java";
73
 
74
    // Create a selector.
75
    VideoSearchSelector selector = new VideoSearchSelector();
76
    selector.setSearchType(VideoSearchSelectorSearchType.VIDEO);
77
    selector.setQuery(queryString);
78
    selector.setPaging(new Paging(0, PAGE_SIZE));
79
 
80
    // Run the query.
81
    VideoSearchPage page = videoService.search(selector);
82
 
83
    // Display videos.
84
    if (page.getTotalNumEntries() > 0) {
85
      for (YouTubeVideo video : page.getEntries()) {
86
        System.out.printf(
87
            "YouTube video ID '%s' with title '%s' found.\n", video.getId(), video.getTitle());
88
      }
89
      System.out.printf("\tTotal number of matching videos: %d.\n", page.getTotalNumEntries());
90
    } else {
91
      System.out.printf("No videos matching '%s' were found.\n", queryString);
92
    }
93
  }
94
}