You Tube视频上传" 401 Unauthorized"使用youtube-data-api

时间:2014-08-08 15:41:37

标签: java youtube youtube-api youtube-data-api

我正在尝试使用java youtube api将视频上传到Youtube并获取" 401 Unauthorized"。

我使用开发者控制台创建了一个服务帐户并使用该.p12文件

这是我正在使用的代码。

public class YouTubeUtils {

private static YouTube youtube;
private static final String VIDEO_FORMAT = "video/*";
private static final String APPLICATION_NAME = "TestApp";

private static final String SERVICE_ACCOUNT_EMAIL = "xxxxx@developer.gserviceaccount.com";
private static HttpTransport httpTransport;
private static final JsonFactory JSON_FACTORY = new JacksonFactory();

public static void main(String[] args) {
    try {
        try {
            httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            if (SERVICE_ACCOUNT_EMAIL.startsWith("Enter ")) {
                System.err.println(SERVICE_ACCOUNT_EMAIL);
                System.exit(1);
            }
            String p12Content = Files.readFirstLine(new File("C:/Workspace/TestApp.p12"),
                    Charset.defaultCharset());
            if (p12Content.startsWith("Please")) {
                System.err.println(p12Content);
                System.exit(1);
            }
            List<String> scopes = Lists.newArrayList(YouTubeScopes.YOUTUBE_UPLOAD);

            GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
            .setServiceAccountScopes(scopes)
            .setServiceAccountPrivateKeyFromP12File(new File("C:/Workspace/TestApp.p12"))
            .build();


            youtube = new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
                    .setApplicationName(APPLICATION_NAME).build();
            Video videoObjectDefiningMetadata = new Video();

            VideoStatus status = new VideoStatus();
            status.setPrivacyStatus("public");
            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("video");
            snippet.setTags(tags);

            videoObjectDefiningMetadata.setSnippet(snippet);
            FileInputStream fin = new FileInputStream(new File("C:/Workspace/small.mp4"));

            InputStreamContent mediaContent = new InputStreamContent(VIDEO_FORMAT,fin);

            YouTube.Videos.Insert videoInsert = youtube.videos().insert(
                    "snippet,statistics,status",
                    videoObjectDefiningMetadata, mediaContent);
            MediaHttpUploader uploader = videoInsert.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 (IOException e) {
            e.printStackTrace();
            System.err.println(e.getMessage());
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

}

打印

启动启动

启动完成

之后我得到了#34; 401 Unauthorized&#34;

有人可以帮我解决这个问题吗。

由于

1 个答案:

答案 0 :(得分:0)

请参阅this链接,上面写着 Service Accounts do not work with the YouTube API

此外,请参阅this链接,了解如何获取授权凭据以及Youtube API支持的凭据类型。

here是使用OAuth 2.0将视频上传到youtube的示例。