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



我正在尝试使用java Youtube api上传视频到Youtube并获得"401未经授权"。

我已经使用开发人员控制台和使用。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();
    }
}

}

打印

启动开始

初始化完成

之后我得到"401 Unauthorized"

有人能帮我一下吗?

谢谢

请参考这个链接,上面写着 Service Accounts do not work with the YouTube API

另外,请参考此链接了解如何获得授权凭证以及Youtube API支持的凭证类型。

下面是一个使用OAuth 2.0上传视频到youtube的例子

相关内容

  • 没有找到相关文章

最新更新