代码之家  ›  专栏  ›  技术社区  ›  Anand

youtube视频上传“401未经授权”使用youtube数据api

  •  0
  • Anand  · 技术社区  · 10 年前

    我正在尝试使用javayoutubeapi将视频上传到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未授权”

    有人能帮我吗。

    谢谢

    1 回复  |  直到 10 年前
        1
  •  0
  •   user3487063    10 年前

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

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

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