From 4d73ffd4bfe39bae6fc81fcc6659e4cf2dae619e Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 7 Dec 2018 23:24:58 +0100 Subject: [PATCH] Finally successfully getting data --- TLAWords.iml | 2 + pom.xml | 50 +++++++++++++++++ src/main/java/Main.java | 120 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 TLAWords.iml create mode 100644 pom.xml create mode 100644 src/main/java/Main.java diff --git a/TLAWords.iml b/TLAWords.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/TLAWords.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..39dd886 --- /dev/null +++ b/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + io.github.norbipeti + TLAWords + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + com.google.api-client + google-api-client + 1.22.0 + + + com.google.oauth-client + google-oauth-client-jetty + 1.22.0 + + + com.google.code.gson + gson + 1.7.2 + + + com.fasterxml.jackson.core + jackson-databind + 2.4.4 + + + com.google.apis + google-api-services-youtube + v3-rev182-1.22.0 + + + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..40fda58 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,120 @@ +// Sample Java code for user authorization + +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.store.FileDataStoreFactory; +import com.google.api.services.youtube.YouTube; +import com.google.api.services.youtube.model.VideoListResponse; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; + + +public class Main { + + /** Application name. */ + private static final String APPLICATION_NAME = "API Sample"; + + /** Directory to store user credentials for this application. */ + private static final java.io.File DATA_STORE_DIR = new java.io.File( + System.getProperty("user.home"), ".credentials/java-youtube-api-tests"); + + /** Global instance of the {@link FileDataStoreFactory}. */ + private static FileDataStoreFactory DATA_STORE_FACTORY; + + /** Global instance of the JSON factory. */ + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + + /** Global instance of the HTTP transport. */ + private static HttpTransport HTTP_TRANSPORT; + + /** + * Global instance of the scopes required by this quickstart. + *

+ * If modifying these scopes, delete your previously saved credentials + * at ~/.credentials/drive-java-quickstart + */ + private static final Collection SCOPES = Arrays.asList("https://www.googleapis.com/auth/youtube.readonly"); + + static { + try { + HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); + } catch (Throwable t) { + t.printStackTrace(); + System.exit(1); + } + } + + /** + * Creates an authorized Credential object. + * + * @return an authorized Credential object. + * @throws IOException + */ + public static Credential authorize() throws IOException { + //System.setProperty("GOOGLE_APPLICATION_CREDENTIALS", "src/resources/client_secret.json"); + System.out.println(new File(".").getAbsolutePath()); + return GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY).createScoped(SCOPES); + } + + /** + * Build and return an authorized API client service, such as a YouTube + * Data API client service. + * + * @return an authorized API client service + * @throws IOException + */ + public static YouTube getYouTubeService() throws IOException { + Credential credential = authorize(); + + return new YouTube.Builder( + HTTP_TRANSPORT, JSON_FACTORY, credential) + .setApplicationName(APPLICATION_NAME) + .build(); + } + + public static void main(String[] args) throws IOException { + + YouTube youtube = getYouTubeService(); + + try { + HashMap parameters = new HashMap<>(); + parameters.put("part", "snippet,contentDetails,statistics"); + parameters.put("chart", "mostPopular"); + parameters.put("regionCode", "US"); + parameters.put("videoCategoryId", ""); + + YouTube.Videos.List videosListMostPopularRequest = youtube.videos().list(parameters.get("part").toString()); + if (parameters.containsKey("chart") && parameters.get("chart") != "") { + videosListMostPopularRequest.setChart(parameters.get("chart").toString()); + } + + if (parameters.containsKey("regionCode") && parameters.get("regionCode") != "") { + videosListMostPopularRequest.setRegionCode(parameters.get("regionCode").toString()); + } + + if (parameters.containsKey("videoCategoryId") && parameters.get("videoCategoryId") != "") { + videosListMostPopularRequest.setVideoCategoryId(parameters.get("videoCategoryId").toString()); + } + + VideoListResponse response = videosListMostPopularRequest.execute(); + System.out.println(response); + + + } catch (GoogleJsonResponseException e) { + e.printStackTrace(); + System.err.println("There was a service error: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage()); + } catch (Throwable t) { + t.printStackTrace(); + } + } +} \ No newline at end of file