【发布时间】:2020-12-12 12:57:33
【问题描述】:
我目前正在尝试学习如何使用 Youtube API 和其他东西,但我遇到了一些 NullPointerException 错误,我就是不知道为什么...
我已经尝试了在文档中可以找到的所有内容,但没有任何改变。
private static final String APPLICATION_NAME = "API code samples";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/**
* Create an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize(final NetHttpTransport httpTransport) throws IOException {
// Load client secrets.
InputStream in = CreateBroadcast.class.getResourceAsStream(CLIENT_SECRETS);
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
.build();
Credential credential =
new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
return credential;
}
/**
* Build and return an authorized API client service.
*
* @return an authorized API client service
* @throws GeneralSecurityException, IOException
*/
public static YouTube getService() throws GeneralSecurityException, IOException {
final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Credential credential = authorize(httpTransport);
return new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
/**
* Call function to create API service object. Define and
* execute API request. Print API response.
*
* @throws GeneralSecurityException, IOException, GoogleJsonResponseException
*/
public static void main(String[] args)
throws GeneralSecurityException, IOException, GoogleJsonResponseException {
YouTube youtubeService = getService();
// Define the LiveBroadcast object, which will be uploaded as the request body.
LiveBroadcast liveBroadcast = new LiveBroadcast();
// Add the contentDetails object property to the LiveBroadcast object.
LiveBroadcastContentDetails contentDetails = new LiveBroadcastContentDetails();
contentDetails.setEnableClosedCaptions(true);
contentDetails.setEnableContentEncryption(true);
contentDetails.setEnableDvr(true);
contentDetails.setEnableEmbed(true);
contentDetails.setRecordFromStart(true);
contentDetails.setStartWithSlate(true);
liveBroadcast.setContentDetails(contentDetails);
// Add the snippet object property to the LiveBroadcast object.
LiveBroadcastSnippet snippet = new LiveBroadcastSnippet();
snippet.setScheduledStartTime(new DateTime("2024-01-30T00:00:00.000Z"));
snippet.setTitle("Test broadcast");
liveBroadcast.setSnippet(snippet);
// Add the status object property to the LiveBroadcast object.
LiveBroadcastStatus status = new LiveBroadcastStatus();
status.setPrivacyStatus("unlisted");
liveBroadcast.setStatus(status);
// Define and execute the API request
YouTube.LiveBroadcasts.Insert request = youtubeService.liveBroadcasts()
.insert("snippet,contentDetails,status", liveBroadcast);
LiveBroadcast response = request.execute();
System.out.println(response);
}
这些是我不断收到的错误:
Exception in thread "main" java.lang.NullPointerException
at java.base/java.io.Reader.<init>(Reader.java:167)
at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at com.google.api.services.samples.youtube.cmdline.live.CreateBroadcast.authorize(CreateBroadcast.java:47)
at com.google.api.services.samples.youtube.cmdline.live.CreateBroadcast.getService(CreateBroadcast.java:65)
at com.google.api.services.samples.youtube.cmdline.live.CreateBroadcast.main(CreateBroadcast.java:79)
如果这是一个愚蠢的问题,我很抱歉 :) 在此先感谢蒂洛
【问题讨论】:
-
能否请您添加整个异常堆栈跟踪?
-
你在哪里初始化 CLIENT_SECRETS?检查它是否可以访问客户端机密文件。
标签: java google-api youtube-api youtube-data-api google-api-java-client