我希望创建一个服务器应用程序来检查所述服务器上的文件夹,并自动上传已放入所述文件夹的所有视频文件。服务器是远程的,不会发生人为干预。
在阅读Youtube的API v3指南后,我得出结论,我需要一个“服务帐户”才能上传这些视频,因为应用程序会不断监视此文件夹。我的问题是:是否可以使用服务帐户上传视频,然后将视频放在我的个人YouTube帐户下?
如果这实际上是可能的;我创建了以下代码,但遇到401 Unauthorized(Invalid Token)错误。
我有以下可编辑的来源;我试图让它尽可能小:
package PACKAGE;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.googleapis.media.MediaHttpUploader;
import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener;
import com.google.api.client.http.HttpTransport;
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.YouTubeScopes;
import com.google.api.services.youtube.model.PlaylistItem;
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.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
public class BigQueryTestApp {
private static final String VIDEO_FILE_FORMAT = "video/*";
private static final String SAMPLE_VIDEO_FILENAME = "sample_video.mp4";
public static void main(String[] args) throws IOException, InterruptedException, GeneralSecurityException {
final HttpTransport TRANSPORT = new NetHttpTransport();
final JsonFactory JSON_FACTORY = new JacksonFactory();
File privateKeyFile = new File("privatekey.p12");
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("XXXX@developer.gserviceaccount.com")
.setServiceAccountScopes(YouTubeScopes.all())
.setServiceAccountPrivateKeyFromP12File(privateKeyFile)
.build();
try
{
YouTube youtube = new YouTube.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("CHANGE_IT").build();
Video videoObjectDefiningMetadata = new Video();
VideoStatus status = new VideoStatus();
status.setPrivacyStatus("unlisted");
videoObjectDefiningMetadata.setStatus(status);
VideoSnippet snippet = new VideoSnippet();
Calendar cal = Calendar.getInstance();
snippet.setTitle("Test Upload via Java on " + cal.getTime());
snippet.setDescription(
"Video uploaded via YouTube Data API V3 using the Java library " + "on " + cal.getTime());
List<String> tags = new ArrayList<String>();
tags.add("test");
tags.add("example");
tags.add("java");
tags.add("YouTube Data API V3");
tags.add("erase me");
snippet.setTags(tags);
videoObjectDefiningMetadata.setSnippet(snippet);
InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT,
BigQueryTestApp.class.getResourceAsStream(SAMPLE_VIDEO_FILENAME));
YouTube.Videos.Insert videoInsert = youtube.videos().insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);
MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();//thumbnailSet.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
public void progressChanged(MediaHttpUploader uploader) throws IOException
{
switch (uploader.getUploadState())
{
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: " + uploader.getProgress());
break;
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
case NOT_STARTED:
System.out.println("Upload Not Started!");
break;
}
}
};
uploader.setProgressListener(progressListener);
// Call the API and upload the video.
Video returnedVideo = videoInsert.execute();
// Print data about the newly inserted video from the API response.
System.out.println("\n================== Returned Video ==================\n");
System.out.println(" - Id: " + returnedVideo.getId());
System.out.println(" - Title: " + returnedVideo.getSnippet().getTitle());
System.out.println(" - Tags: " + returnedVideo.getSnippet().getTags());
System.out.println(" - Privacy Status: " + returnedVideo.getStatus().getPrivacyStatus());
System.out.println(" - Video Count: " + returnedVideo.getStatistics().getViewCount());
}
catch (GoogleJsonResponseException e) {
System.out.println(e.getMessage());
System.out.println("Headers: "+e.getHeaders().toString());
}
最后,我读了很多关于服务帐户的故事。有人说他们与Youtube兼容,其他人说他们不是。这让我感到困惑,我发现了更多关于stackoverflow的问题,这些问题并不是确定无论是否可能。
答案 0 :(得分:0)
YouTube不支持服务帐户,他们也没有计划:
https://code.google.com/p/gdata-issues/issues/detail?id=5370
然后,一个选项是设置您的应用程序以执行初始oAuth流程(您通过一次用户交互启动),然后作为该流程的一部分,您将同时发布临时访问令牌和刷新令牌。刷新令牌可用于获取更多访问令牌,而无需再次运行oAuth流,并且刷新令牌不会自行过期(但可以手动撤销)。因此,您的应用可以存储您的刷新令牌,监控您的文件夹,然后在需要上传新视频时自行获取访问令牌。看到这个链接:
https://developers.google.com/youtube/v3/guides/moving_to_oauth#standalone
(请注意,它特别提到了守护程序的想法,该守护程序监视目录以便上传新视频)。