/**
* Sample Java code for youtube.videos.insert
* See instructions for running these code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#java
*/
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.Video;
import com.google.api.services.youtube.model.VideoSnippet;
import com.google.api.services.youtube.model.VideoStatus;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Collection;
public class ApiExample {
// You need to set this value for your code to compile.
// For example: ... DEVELOPER_KEY = "YOUR ACTUAL KEY";
private static final String DEVELOPER_KEY = "YOUR_API_KEY";
private static final String APPLICATION_NAME = "API code samples";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/**
* 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();
return new YouTube.Builder(httpTransport, JSON_FACTORY, null)
.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 Video object, which will be uploaded as the request body.
Video video = new Video();
// Add the snippet object property to the Video object.
VideoSnippet snippet = new VideoSnippet();
snippet.setCategoryId("22");
snippet.setDescription("Description of uploaded video.");
snippet.setTitle("Test video upload.");
video.setSnippet(snippet);
// Add the status object property to the Video object.
VideoStatus status = new VideoStatus();
status.setPrivacyStatus("private");
video.setStatus(status);
// 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 137438953472.
File mediaFile = new File("YOUR_FILE");
InputStreamContent mediaContent =
new InputStreamContent("application/octet-stream",
new BufferedInputStream(new FileInputStream(mediaFile)));
mediaContent.setLength(mediaFile.length());
// Define and execute the API request
YouTube.Videos.Insert request = youtubeService.videos()
.insert("snippet,status", video, mediaContent);
Video response = request.setKey(DEVELOPER_KEY).execute();
System.out.println(response);
}
}
您还可以查看另一个将视频上传到github上youtube的java代码示例。 请注意,您需要对此进行一些更改: 在安卓系统下,你没有 main . 您将需要(1)将代码放在任何需要的地方,并且您可能希望(2)异步执行。 在使用kotlin时,还需要(3)相应地修改语法。
1条答案
按热度按时间jecbmhm31#
这展示了如何使用http请求上传视频。
java中也有代码片段(kotlin非常类似)。
要查看如何上传视频,请转至https://developers.google.com/youtube/v3/code_samples/code_snippets,选择
Videos
低于Resource
以及insert
在方法下。如果你点击
Show code
并选择JAVA
在顶部,您将看到以下示例代码:您还可以查看另一个将视频上传到github上youtube的java代码示例。
请注意,您需要对此进行一些更改:
在安卓系统下,你没有
main
. 您将需要(1)将代码放在任何需要的地方,并且您可能希望(2)异步执行。在使用kotlin时,还需要(3)相应地修改语法。