无法使用 java 插入视频 youtube
Posted
技术标签:
【中文标题】无法使用 java 插入视频 youtube【英文标题】:Can not insert video youtube using java 【发布时间】:2020-06-10 23:52:58 【问题描述】:请用以下代码解释详细错误: Authen + 路径文件就可以了, 请支持。
public class ApiExample
private static final String CLIENT_SECRETS = "client_secret.json";
private static final Collection<String> SCOPES = Arrays.asList("https://www.googleapis.com/auth/youtube.upload");
private static final String APPLICATION_NAME = "API code samples";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
public static Credential authorize(final NetHttpTransport httpTransport) throws IOException
// Load client secrets.
InputStream in = ApiExample.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;
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();
public static void main(String[] args) throws GeneralSecurityException, IOException, GoogleJsonResponseException
YouTube youtubeService = getService();
// Define the Video object, which will be uploaded as the request body.
Video video = new Video();
VideoStatus status = new VideoStatus();
status.setPrivacyStatus("public");
status.setEmbeddable(true);
status.setLicense("youtube");
video.setStatus(status);
// Add the snippet object property to the Video object.
VideoSnippet snippet = new VideoSnippet();
snippet.setTitle("Chương 5");
// snippet.setChannelId("UCMINtZzALW83UuWpjC_H51Q");
snippet.setDescription("chương 5");
String[] tags = "truyen ngon tinh", ;
snippet.setTags(Arrays.asList(tags));
snippet.setCategoryId("1");
video.setSnippet(snippet);
// TODO: For this request to work, you must replace "YOUR_FILE"
// with a pointer to the actual file you are uploading.
// The maximum file size for this operation is 128GB.
String file = Constants.currentPath + Constants.mp4 + File.separator + "6" + File.separator + "6" + Constants.ext_video_mp4;
File mediaFile = new File(file);
InputStreamContent mediaContent = new InputStreamContent("video/*", new BufferedInputStream(new FileInputStream(mediaFile)));
mediaContent.setLength(mediaFile.length());
// Define and execute the API request
YouTube.Videos.Insert request = youtubeService.videos().insert("processingDetails", video, mediaContent);
Video response = request.setAutoLevels(false).setNotifySubscribers(true).setStabilize(false).execute();
System.out.println(response);
=======================错误
“代码”:400,
“错误”:[
“域”:“youtube.part”,
“位置”:“部分”,
"locationType" : "参数",
“消息”:“0”,
“原因”:“意外部分”
],
“消息”:“0”
谢谢,
【问题讨论】:
请更新您的问题并描述您的代码应该做什么以及您的问题是什么。注意:我们不支持。 请支持。 【参考方案1】:您似乎收到错误类型 400,这意味着 badRequest。从 Youtube 检查此文档并尝试解决它。
有关错误类型,请查看此文档:
https://developers.google.com/youtube/v3/docs/core_errors
【讨论】:
以上是关于无法使用 java 插入视频 youtube的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 YouTube Android Player API 播放某些 Youtube 视频
无法使用YTPlayerView在iOS上播放背景中的YouTube视频