Finally successfully getting data
This commit is contained in:
commit
4d73ffd4bf
3 changed files with 172 additions and 0 deletions
2
TLAWords.iml
Normal file
2
TLAWords.iml
Normal file
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4" />
|
50
pom.xml
Normal file
50
pom.xml
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>io.github.norbipeti</groupId>
|
||||
<artifactId>TLAWords</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.api-client</groupId>
|
||||
<artifactId>google-api-client</artifactId>
|
||||
<version>1.22.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.oauth-client</groupId>
|
||||
<artifactId>google-oauth-client-jetty</artifactId>
|
||||
<version>1.22.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>1.7.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.4.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.apis</groupId>
|
||||
<artifactId>google-api-services-youtube</artifactId>
|
||||
<version>v3-rev182-1.22.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
120
src/main/java/Main.java
Normal file
120
src/main/java/Main.java
Normal file
|
@ -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.
|
||||
* <p>
|
||||
* If modifying these scopes, delete your previously saved credentials
|
||||
* at ~/.credentials/drive-java-quickstart
|
||||
*/
|
||||
private static final Collection<String> 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<String, String> 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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue