Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从android应用程序将youtube上的视频上传到特定帐户的最佳方式是什么?_Java_Video_File Upload_Youtube Data Api - Fatal编程技术网

Java 从android应用程序将youtube上的视频上传到特定帐户的最佳方式是什么?

Java 从android应用程序将youtube上的视频上传到特定帐户的最佳方式是什么?,java,video,file-upload,youtube-data-api,Java,Video,File Upload,Youtube Data Api,我必须使用java将视频从我的android应用程序上传到youtube上的一个特定帐户。你应该使用youtube API(它实际上就是用来上传视频的) 以下是我在Github上找到的一个示例: import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.cli

我必须使用java将视频从我的android应用程序上传到youtube上的一个特定帐户。你应该使用youtube API(它实际上就是用来上传视频的)

以下是我在Github上找到的一个示例:

import com.google.api.client.auth.oauth2.Credential;
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.InputStreamContent;
import com.google.api.services.samples.youtube.cmdline.Auth;
import com.google.api.services.youtube.YouTube;
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 com.google.common.collect.Lists;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Upload a video to the authenticated user's channel. Use OAuth 2.0 to
 * authorize the request. Note that you must add your video files to the
 * project folder to upload them with this application.
 *
 * @author Jeremy Walker
 */
public class UploadVideo {

    /**
     * Define a global instance of a Youtube object, which will be used
     * to make YouTube Data API requests.
     */
    private static YouTube youtube;

    /**
     * Define a global variable that specifies the MIME type of the video
     * being uploaded.
     */
    private static final String VIDEO_FILE_FORMAT = "video/*";

    private static final String SAMPLE_VIDEO_FILENAME = "sample-video.mp4";

    /**
     * Upload the user-selected video to the user's YouTube channel. The code
     * looks for the video in the application's project folder and uses OAuth
     * 2.0 to authorize the API request.
     *
     * @param args command line args (not used).
     */
    public static void main(String[] args) {

        // This OAuth 2.0 access scope allows an application to upload files
        // to the authenticated user's YouTube channel, but doesn't allow
        // other types of access.
        List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.upload");

        try {
            // Authorize the request.
            Credential credential = Auth.authorize(scopes, "uploadvideo");

            // This object is used to make YouTube Data API requests.
            youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName(
                    "youtube-cmdline-uploadvideo-sample").build();

            System.out.println("Uploading: " + SAMPLE_VIDEO_FILENAME);

            // Add extra information to the video before uploading.
            Video videoObjectDefiningMetadata = new Video();

            // Set the video to be publicly visible. This is the default
            // setting. Other supporting settings are "unlisted" and "private."
            VideoStatus status = new VideoStatus();
            status.setPrivacyStatus("public");
            videoObjectDefiningMetadata.setStatus(status);

            // Most of the video's metadata is set on the VideoSnippet object.
            VideoSnippet snippet = new VideoSnippet();

            // This code uses a Calendar instance to create a unique name and
            // description for test purposes so that you can easily upload
            // multiple files. You should remove this code from your project
            // and use your own standard names instead.
            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());

            // Set the keyword tags that you want to associate with the video.
            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);

            // Add the completed snippet object to the video resource.
            videoObjectDefiningMetadata.setSnippet(snippet);

            InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT,
                    UploadVideo.class.getResourceAsStream("/sample-video.mp4"));

            // Insert the video. The command sends three arguments. The first
            // specifies which information the API request is setting and which
            // information the API response should return. The second argument
            // is the video resource that contains metadata about the new video.
            // The third argument is the actual video content.
            YouTube.Videos.Insert videoInsert = youtube.videos()
                    .insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);

            // Set the upload type and add an event listener.
            MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();

            // Indicate whether direct media upload is enabled. A value of
            // "True" indicates that direct media upload is enabled and that
            // the entire media content will be uploaded in a single request.
            // A value of "False," which is the default, indicates that the
            // request will use the resumable media upload protocol, which
            // supports the ability to resume an upload operation after a
            // network interruption or other transmission failure, saving
            // time and bandwidth in the event of network failures.
            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.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
                    + e.getDetails().getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IOException: " + e.getMessage());
            e.printStackTrace();
        } catch (Throwable t) {
            System.err.println("Throwable: " + t.getMessage());
            t.printStackTrace();
        }
    }
}
import com.google.api.client.auth.oauth2.Credential;
导入com.google.api.client.googleapis.json.GoogleJsonResponseException;
导入com.google.api.client.googleapis.media.MediaHttpUploader;
导入com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener;
导入com.google.api.client.http.InputStreamContent;
导入com.google.api.services.samples.youtube.cmdline.Auth;
导入com.google.api.services.youtube.youtube;
导入com.google.api.services.youtube.model.Video;
导入com.google.api.services.youtube.model.VideoSnippet;
导入com.google.api.services.youtube.model.VideoStatus;
导入com.google.common.collect.list;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.Calendar;
导入java.util.List;
/**
*将视频上载到经过身份验证的用户频道。使用OAuth 2.0来
*批准请求。请注意,您必须将视频文件添加到
*用于与此应用程序一起上载的项目文件夹。
*
*@作者杰里米·沃克
*/
公共类上传视频{
/**
*定义将要使用的Youtube对象的全局实例
*发出YouTube数据API请求。
*/
私有静态YouTube YouTube;
/**
*定义一个全局变量,该变量指定视频的MIME类型
*正在上传。
*/
私有静态最终字符串视频文件格式=“视频/*”;
私有静态最终字符串SAMPLE\u VIDEO\u FILENAME=“SAMPLE VIDEO.mp4”;
/**
*将用户选择的视频上载到用户的YouTube频道。代码
*在应用程序的项目文件夹中查找视频并使用OAuth
*2.0授权API请求。
*
*@param args命令行args(未使用)。
*/
公共静态void main(字符串[]args){
//此OAuth 2.0访问范围允许应用程序上载文件
//已验证用户的YouTube频道,但不允许
//其他类型的访问。
列表范围=列表。newArrayList(“https://www.googleapis.com/auth/youtube.upload");
试一试{
//批准请求。
凭证=授权(作用域,“上传视频”);
//此对象用于发出YouTube数据API请求。
youtube=new youtube.Builder(Auth.HTTP_传输、Auth.JSON_工厂、凭证)。setApplicationName(
“youtube cmdline上载视频示例”).build();
System.out.println(“上传:+示例\u视频\u文件名”);
//在上传之前向视频添加额外信息。
视频对象定义元数据=新视频();
//将视频设置为公开可见。这是默认设置
//设置。其他支持设置为“未列出”和“专用”
VideoStatus状态=新的VideoStatus();
状态。setPrivacyStatus(“公共”);
videoObjectDefiningMetadata.setStatus(状态);
//视频的大部分元数据都设置在VideoSnippet对象上。
VideoSnippet snippet=新的VideoSnippet();
//此代码使用日历实例创建唯一的名称和名称
//用于测试目的的说明,以便您可以轻松上载
//多个文件。您应该从项目中删除此代码
//用你自己的标准名字代替。
Calendar cal=Calendar.getInstance();
setTitle(“在“+cal.getTime()上通过Java测试上传”);
snippet.setDescription(
使用“+cal.getTime()上的Java库“+”通过YouTube数据API V3上传的视频;
//设置要与视频关联的关键字标记。
列表标记=新的ArrayList();
添加(“测试”);
添加(“示例”);
tags.add(“java”);
添加(“YouTube数据API V3”);
标签。添加(“删除我”);
setTags(标签);
//将完成的代码段对象添加到视频资源。
videoObjectDefiningMetadata.setSnippet(代码段);
InputStreamContent mediaContent=新的InputStreamContent(视频文件格式,
UploadVideo.class.getResourceAsStream(“/sample video.mp4”);
//插入视频。该命令发送三个参数。第一个参数
//指定API请求正在设置的信息以及
//API响应应返回的信息。第二个参数
//是包含新视频元数据的视频资源。
//第三个论点是实际的视频内容。
YouTube.Videos.Insert videoInsert=YouTube.Videos()
.插入(“片段、统计、状态”、videoObjectDefiningMetadata、mediaContent);
//设置上载类型并添加事件侦听器。
MediaHttpUploader uploader=videoInsert.getMediaHttpUploader();
//指示是否启用直接媒体上载。值为
//“True”表示已启用直接媒体上载,并且
//整个媒体内容将在一次请求中上载。
//默认值为“False”,表示
//请求将使用可恢复媒体上载协议,该协议
//支持在上传后恢复上传操作的功能
//网络中断或其他传输故障,保存
//发生网络故障时的时间和带宽。
uploader.setDirectUploadeEnabled(false);
MediaHttpUploaderProgressListener=新的MediaHttpUploaderProgressListener(){