无法使用Java插入YouTube视频

问题描述 投票:-3回答:1

请在下面的代码中说明详细错误:验证+路径文件没问题,请支持。

public class ApiExample {    
    private static final String CLIENT_SECRETS = "client_secret.json";    
    private static final Collection<String> SCOPES = Arrays.asList("https://www.googleapis.com/auth/youtube.upload");    
    private static final String APPLICATION_NAME = "API code samples";    
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();   
    public static Credential authorize(final NetHttpTransport httpTransport) throws IOException {    
        // Load client secrets.    
        InputStream in = ApiExample.class.getResourceAsStream(CLIENT_SECRETS);    
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));    
        // Build flow and trigger user authorization request.    
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES).build();    
        Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");    
        return credential;    
    }
    public static YouTube getService() throws GeneralSecurityException, IOException {    
        final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();    
        Credential credential = authorize(httpTransport);    
        return new YouTube.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();    
    }    
    public static void main(String[] args) throws GeneralSecurityException, IOException, GoogleJsonResponseException {    
        YouTube youtubeService = getService();    
        // Define the Video object, which will be uploaded as the request body.    
        Video video = new Video();    
        VideoStatus status = new VideoStatus();    
        status.setPrivacyStatus("public");    
        status.setEmbeddable(true);    
        status.setLicense("youtube");    
        video.setStatus(status);    
        // Add the snippet object property to the Video object.    
        VideoSnippet snippet = new VideoSnippet();    
        snippet.setTitle("Chương 5");    
        // snippet.setChannelId("UCMINtZzALW83UuWpjC_H51Q");    
        snippet.setDescription("chương 5");    
        String[] tags = { "truyen ngon tinh", };    
        snippet.setTags(Arrays.asList(tags));    
        snippet.setCategoryId("1");    
        video.setSnippet(snippet);    
        // TODO: For this request to work, you must replace "YOUR_FILE"    
        // with a pointer to the actual file you are uploading.    
        // The maximum file size for this operation is 128GB.    
        String file = Constants.currentPath + Constants.mp4 + File.separator + "6" + File.separator + "6" + Constants.ext_video_mp4;    
        File mediaFile = new File(file);    
        InputStreamContent mediaContent = new InputStreamContent("video/*", new BufferedInputStream(new FileInputStream(mediaFile)));    
        mediaContent.setLength(mediaFile.length());    
        // Define and execute the API request    
        YouTube.Videos.Insert request = youtubeService.videos().insert("processingDetails", video, mediaContent);    
        Video response = request.setAutoLevels(false).setNotifySubscribers(true).setStabilize(false).execute();    
        System.out.println(response);    
    }    
}        

======================错误

“代码”:400,

“错误”:[{

“域”:“ youtube.part”,

“位置”:“部分”,

“ locationType”:“ parameter”,

“消息”:“ {0}”,

“原因”:“ unexpectedPart”

}],

“消息”:“ {0}”

谢谢,

java google-api youtube-data-api
1个回答
0
投票

似乎您收到错误类型400,这意味着badRequest。从YouTube上查看此文档,并尝试解决它。​​

enter image description here

有关错误类型,请查看此文档:

https://developers.google.com/youtube/v3/docs/core_errors

© www.soinside.com 2019 - 2024. All rights reserved.