Skip to content

Commit 3b1fea3

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/reporting/CreateReportingJob.java"
1 parent 1c6f104 commit 3b1fea3

1 file changed

Lines changed: 176 additions & 0 deletions

File tree

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* Copyright (c) 2015 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. 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 distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.services.samples.youtube.cmdline.reporting;
16+
17+
import com.google.api.client.auth.oauth2.Credential;
18+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
19+
import com.google.api.services.samples.youtube.cmdline.Auth;
20+
import com.google.api.services.youtubereporting.YouTubeReporting;
21+
import com.google.api.services.youtubereporting.model.Job;
22+
import com.google.api.services.youtubereporting.model.ListReportTypesResponse;
23+
import com.google.api.services.youtubereporting.model.ReportType;
24+
import com.google.common.collect.Lists;
25+
26+
import java.io.BufferedReader;
27+
import java.io.IOException;
28+
import java.io.InputStreamReader;
29+
import java.util.List;
30+
31+
/**
32+
* This sample creates a reporting job by:
33+
*
34+
* 1. Listing the available report types using the "reportTypes.list" method.
35+
* 2. Creating a reporting job using the "jobs.create" method.
36+
*
37+
* @author Ibrahim Ulukaya
38+
*/
39+
public class CreateReportingJob {
40+
41+
/**
42+
* Define a global instance of a YouTube Reporting object, which will be used to make
43+
* YouTube Reporting API requests.
44+
*/
45+
private static YouTubeReporting youtubeReporting;
46+
47+
48+
/**
49+
* Create a reporting job.
50+
*
51+
* @param args command line args (not used).
52+
*/
53+
public static void main(String[] args) {
54+
55+
/*
56+
* This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for
57+
* authenticated user's account. Any request that retrieves earnings or ad performance metrics must
58+
* use this scope.
59+
*/
60+
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/yt-analytics-monetary.readonly");
61+
62+
try {
63+
// Authorize the request.
64+
Credential credential = Auth.authorize(scopes, "createreportingjob");
65+
66+
// This object is used to make YouTube Reporting API requests.
67+
youtubeReporting = new YouTubeReporting.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
68+
.setApplicationName("youtube-cmdline-createreportingjob-sample").build();
69+
70+
// Prompt the user to specify the name of the job to be created.
71+
String name = getNameFromUser();
72+
73+
if (listReportTypes()) {
74+
createReportingJob(getReportTypeIdFromUser(), name);
75+
}
76+
} catch (GoogleJsonResponseException e) {
77+
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode()
78+
+ " : " + e.getDetails().getMessage());
79+
e.printStackTrace();
80+
81+
} catch (IOException e) {
82+
System.err.println("IOException: " + e.getMessage());
83+
e.printStackTrace();
84+
} catch (Throwable t) {
85+
System.err.println("Throwable: " + t.getMessage());
86+
t.printStackTrace();
87+
}
88+
}
89+
90+
/**
91+
* Lists report types. (reportTypes.listReportTypes)
92+
* @return true if at least one report type exists
93+
* @throws IOException
94+
*/
95+
private static boolean listReportTypes() throws IOException {
96+
// Call the YouTube Reporting API's reportTypes.list method to retrieve report types.
97+
ListReportTypesResponse reportTypesListResponse = youtubeReporting.reportTypes().list()
98+
.execute();
99+
List<ReportType> reportTypeList = reportTypesListResponse.getReportTypes();
100+
101+
if (reportTypeList == null || reportTypeList.isEmpty()) {
102+
System.out.println("No report types found.");
103+
return false;
104+
} else {
105+
// Print information from the API response.
106+
System.out.println("\n================== Report Types ==================\n");
107+
for (ReportType reportType : reportTypeList) {
108+
System.out.println(" - Id: " + reportType.getId());
109+
System.out.println(" - Name: " + reportType.getName());
110+
System.out.println("\n-------------------------------------------------------------\n");
111+
}
112+
}
113+
return true;
114+
}
115+
116+
/**
117+
* Creates a reporting job. (jobs.create)
118+
*
119+
* @param reportTypeId Id of the job's report type.
120+
* @param name name of the job.
121+
* @throws IOException
122+
*/
123+
private static void createReportingJob(String reportTypeId, String name)
124+
throws IOException {
125+
// Create a reporting job with a name and a report type id.
126+
Job job = new Job();
127+
job.setReportTypeId(reportTypeId);
128+
job.setName(name);
129+
130+
// Call the YouTube Reporting API's jobs.create method to create a job.
131+
Job createdJob = youtubeReporting.jobs().create(job).execute();
132+
133+
// Print information from the API response.
134+
System.out.println("\n================== Created reporting job ==================\n");
135+
System.out.println(" - ID: " + createdJob.getId());
136+
System.out.println(" - Name: " + createdJob.getName());
137+
System.out.println(" - Report Type Id: " + createdJob.getReportTypeId());
138+
System.out.println(" - Create Time: " + createdJob.getCreateTime());
139+
System.out.println("\n-------------------------------------------------------------\n");
140+
}
141+
142+
/*
143+
* Prompt the user to enter a name for the job. Then return the name.
144+
*/
145+
private static String getNameFromUser() throws IOException {
146+
147+
String name = "";
148+
149+
System.out.print("Please enter the name for the job [javaTestJob]: ");
150+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
151+
name = bReader.readLine();
152+
153+
if (name.length() < 1) {
154+
// If nothing is entered, defaults to "javaTestJob".
155+
name = "javaTestJob";
156+
}
157+
158+
System.out.println("You chose " + name + " as the name for the job.");
159+
return name;
160+
}
161+
162+
/*
163+
* Prompt the user to enter a report type id for the job. Then return the id.
164+
*/
165+
private static String getReportTypeIdFromUser() throws IOException {
166+
167+
String id = "";
168+
169+
System.out.print("Please enter the reportTypeId for the job: ");
170+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
171+
id = bReader.readLine();
172+
173+
System.out.println("You chose " + id + " as the report type Id for the job.");
174+
return id;
175+
}
176+
}

0 commit comments

Comments
 (0)