如何用Android Studio通过YouTube Data V3 API上传视频
使用YouTube Data V3 API上传视频的Java实现示例
1. 添加依赖
在Maven项目中引入Google客户端库依赖:
<dependencies> <dependency> <groupId>com.google.apis</groupId> <artifactId>google-api-services-youtube</artifactId> <version>v3-rev20240215-2.0.0</version> </dependency> <dependency> <groupId>com.google.oauth-client</groupId> <artifactId>google-oauth-client-jetty</artifactId> <version>1.40.1</version> </dependency> <dependency> <groupId>com.google.http-client</groupId> <artifactId>google-http-client-gson</artifactId> <version>1.43.3</version> </dependency> </dependencies>
2. 核心上传代码
以下是完整的视频上传实现,包含OAuth授权和视频元数据配置:
import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.FileContent; import com.google.api.client.json.gson.GsonFactory; 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.IOException; import java.io.InputStreamReader; import java.security.GeneralSecurityException; import java.util.Arrays; import java.util.List; public class YouTubeUploader { // 定义API所需权限 private static final List<String> SCOPES = Arrays.asList("https://www.googleapis.com/auth/youtube.upload"); private static final String APPLICATION_NAME = "你的应用名称"; // 客户端密钥文件路径(放在项目resources目录下) private static final String CLIENT_SECRETS_PATH = "/client_secrets.json"; public static void main(String[] args) throws GeneralSecurityException, IOException { // 初始化HTTP传输和JSON工厂 var httpTransport = GoogleNetHttpTransport.newTrustedTransport(); var jsonFactory = GsonFactory.getDefaultInstance(); // 加载客户端密钥 var clientSecrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(YouTubeUploader.class.getResourceAsStream(CLIENT_SECRETS_PATH))); // 构建授权流程 var flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, jsonFactory, clientSecrets, SCOPES) .setAccessType("offline") .setApprovalPrompt("force") .build(); // 启动本地服务器接收授权回调 var receiver = new LocalServerReceiver.Builder().setPort(8080).build(); Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); // 初始化YouTube服务实例 var youtubeService = new YouTube.Builder(httpTransport, jsonFactory, credential) .setApplicationName(APPLICATION_NAME) .build(); // 配置视频元数据 var videoSnippet = new VideoSnippet(); videoSnippet.setTitle("测试上传视频标题"); videoSnippet.setDescription("通过YouTube Data V3 API上传的测试视频"); videoSnippet.setTags(Arrays.asList("测试", "API上传")); videoSnippet.setCategoryId("22"); // 分类ID,22对应People & Blogs,可按需修改 // 设置视频隐私状态(public/private/unlisted) var videoStatus = new VideoStatus(); videoStatus.setPrivacyStatus("unlisted"); // 组装视频对象 var video = new Video(); video.setSnippet(videoSnippet); video.setStatus(videoStatus); // 指定待上传的视频文件 var videoFile = new java.io.File("/path/to/your/video.mp4"); var mediaContent = new FileContent("video/*", videoFile); // 执行上传请求 var insertRequest = youtubeService.videos().insert(Arrays.asList("snippet", "status"), video, mediaContent); insertRequest.setNotifySubscribers(true); // 是否通知订阅者 // 获取上传结果 var uploadedVideo = insertRequest.execute(); System.out.println("视频上传完成,视频ID:" + uploadedVideo.getId()); System.out.println("视频链接:https://www.youtube.com/watch?v=" + uploadedVideo.getId()); } }
3. 关键注意事项
- 客户端密钥配置:将Google Cloud Console生成的对应Java应用类型的client id和secret保存为
client_secrets.json,放在项目resources目录下;若适配安卓端,需调整为安卓授权流程。 - 权限申请:必须启用
https://www.googleapis.com/auth/youtube.upload权限,否则无法完成上传操作。 - 视频格式要求:仅支持YouTube兼容格式(如MP4),文件大小不超过128GB、时长不超过12小时。
- OAuth授权流程:首次运行会自动打开浏览器引导授权,授权后生成凭证文件,后续运行无需重复授权。
内容的提问来源于stack exchange,提问作者max 123
相关产品推荐
相关产品推荐

