Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit b9e6a9a

Browse files
Add file for coming-soon Java quickstart guide.
1 parent f66c00a commit b9e6a9a

File tree

1 file changed

+119
-0
lines changed
  • java/src/main/java/com/google/api/services/samples/youtube/cmdline/data

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import com.google.api.client.auth.oauth2.Credential;
2+
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
3+
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
4+
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
5+
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
6+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
7+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
8+
import com.google.api.client.http.HttpTransport;
9+
import com.google.api.client.json.jackson2.JacksonFactory;
10+
import com.google.api.client.json.JsonFactory;
11+
import com.google.api.client.util.store.FileDataStoreFactory;
12+
13+
import com.google.api.services.youtube.YouTubeScopes;
14+
import com.google.api.services.youtube.model.*;
15+
import com.google.api.services.youtube.YouTube;
16+
17+
import java.io.IOException;
18+
import java.io.InputStream;
19+
import java.io.InputStreamReader;
20+
import java.util.Arrays;
21+
import java.util.List;
22+
23+
24+
public class Quickstart {
25+
26+
/** Application name. */
27+
private static final String APPLICATION_NAME = "API Sample";
28+
29+
/** Directory to store user credentials for this application. */
30+
private static final java.io.File DATA_STORE_DIR = new java.io.File(
31+
System.getProperty("user.home"), ".credentials/youtube-java-quickstart");
32+
33+
/** Global instance of the {@link FileDataStoreFactory}. */
34+
private static FileDataStoreFactory DATA_STORE_FACTORY;
35+
36+
/** Global instance of the JSON factory. */
37+
private static final JsonFactory JSON_FACTORY =
38+
JacksonFactory.getDefaultInstance();
39+
40+
/** Global instance of the HTTP transport. */
41+
private static HttpTransport HTTP_TRANSPORT;
42+
43+
/** Global instance of the scopes required by this quickstart.
44+
*
45+
* If modifying these scopes, delete your previously saved credentials
46+
* at ~/.credentials/drive-java-quickstart
47+
*/
48+
private static final List<String> SCOPES =
49+
Arrays.asList(YouTubeScopes.YOUTUBE_READONLY);
50+
51+
static {
52+
try {
53+
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
54+
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
55+
} catch (Throwable t) {
56+
t.printStackTrace();
57+
System.exit(1);
58+
}
59+
}
60+
61+
/**
62+
* Create an authorized Credential object.
63+
* @return an authorized Credential object.
64+
* @throws IOException
65+
*/
66+
public static Credential authorize() throws IOException {
67+
// Load client secrets.
68+
InputStream in =
69+
Quickstart.class.getResourceAsStream("/client_secret.json");
70+
GoogleClientSecrets clientSecrets =
71+
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
72+
73+
// Build flow and trigger user authorization request.
74+
GoogleAuthorizationCodeFlow flow =
75+
new GoogleAuthorizationCodeFlow.Builder(
76+
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
77+
.setDataStoreFactory(DATA_STORE_FACTORY)
78+
.setAccessType("offline")
79+
.build();
80+
Credential credential = new AuthorizationCodeInstalledApp(
81+
flow, new LocalServerReceiver()).authorize("user");
82+
return credential;
83+
}
84+
85+
/**
86+
* Build and return an authorized API client service, such as a YouTube
87+
* Data API client service.
88+
* @return an authorized API client service
89+
* @throws IOException
90+
*/
91+
public static YouTube getYouTubeService() throws IOException {
92+
Credential credential = authorize();
93+
return new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
94+
.setApplicationName(APPLICATION_NAME)
95+
.build();
96+
}
97+
98+
public static void main(String[] args) throws IOException {
99+
YouTube youtube = getYouTubeService();
100+
try {
101+
YouTube.Channels.List channelsListByUsernameRequest = youtube.channels().list("snippet,contentDetails,statistics");
102+
channelsListByUsernameRequest.setForUsername("GoogleDevelopers");
103+
104+
ChannelListResponse response = channelsListByUsernameRequest.execute();
105+
Channel channel = response.getItems().get(0);
106+
System.out.printf(
107+
"This channel's ID is %s. Its title is '%s', and it has %s views.\n",
108+
channel.getId(),
109+
channel.getSnippet().getTitle(),
110+
channel.getStatistics().getViewCount());
111+
} catch (GoogleJsonResponseException e) {
112+
e.printStackTrace();
113+
System.err.println("There was a service error: " +
114+
e.getDetails().getCode() + " : " + e.getDetails().getMessage());
115+
} catch (Throwable t) {
116+
t.printStackTrace();
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)