【问题标题】:How to upload videos on Youtube to a particular youtube account on Android programatically如何以编程方式将 Youtube 上的视频上传到 Android 上的特定 youtube 帐户
【发布时间】:2021-03-19 19:23:57
【问题描述】:

这是我的用例: 我希望用户能够将视频内容从他们的设备直接上传到我的 youtube 或专用的 youtube 帐户,而无需离开我的应用程序。首先,这可能吗?请我欣赏代码示例。谢谢

【问题讨论】:

  • 使用 youtube API 怎么样?这方面的文档可以在here 和android 快速入门here 中找到。
  • 其实this part of the docs 听起来更有希望。
  • 谢谢,我已经检查过了,但不幸的是,它对我没有帮助,因为我的用例没有直接的解决方案。这个developers.google.com/youtube/v3/quickstart/android只解释了python的实现
  • 你刚才写的链接显示的不是python而是java/android的例子。

标签: java android kotlin youtube


【解决方案1】:

This 展示了如何使用 HTTP 请求上传视频。

code snippets 在 java 中也可用(和 kotlin 非常相似)。

要查看如何上传视频,请转到https://developers.google.com/youtube/v3/code_samples/code_snippets,在Resource 下选择Videos,在方法下选择insert

如果你然后点击Show code并选择顶部的JAVA,你会得到这个示例代码:

/**
 * Sample Java code for youtube.videos.insert
 * See instructions for running these code samples locally:
 * https://developers.google.com/explorer-help/guides/code_samples#java
 */

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;

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 java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Collection;

public class ApiExample {
    // You need to set this value for your code to compile.
    // For example: ... DEVELOPER_KEY = "YOUR ACTUAL KEY";
    private static final String DEVELOPER_KEY = "YOUR_API_KEY";

    private static final String APPLICATION_NAME = "API code samples";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    /**
     * Build and return an authorized API client service.
     *
     * @return an authorized API client service
     * @throws GeneralSecurityException, IOException
     */
    public static YouTube getService() throws GeneralSecurityException, IOException {
        final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        return new YouTube.Builder(httpTransport, JSON_FACTORY, null)
            .setApplicationName(APPLICATION_NAME)
            .build();
    }

    /**
     * Call function to create API service object. Define and
     * execute API request. Print API response.
     *
     * @throws GeneralSecurityException, IOException, GoogleJsonResponseException
     */
    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();
        
        // Add the snippet object property to the Video object.
        VideoSnippet snippet = new VideoSnippet();
        snippet.setCategoryId("22");
        snippet.setDescription("Description of uploaded video.");
        snippet.setTitle("Test video upload.");
        video.setSnippet(snippet);
        
        // Add the status object property to the Video object.
        VideoStatus status = new VideoStatus();
        status.setPrivacyStatus("private");
        video.setStatus(status);

        // 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 137438953472.
        File mediaFile = new File("YOUR_FILE");
        InputStreamContent mediaContent =
            new InputStreamContent("application/octet-stream",
                new BufferedInputStream(new FileInputStream(mediaFile)));
        mediaContent.setLength(mediaFile.length());

        // Define and execute the API request
        YouTube.Videos.Insert request = youtubeService.videos()
            .insert("snippet,status", video, mediaContent);
        Video response = request.setKey(DEVELOPER_KEY).execute();
        System.out.println(response);
    }
}

你也可以看看另一个example java code for uploading a video to YouTube on GitHub

请注意,您需要对此进行一些更改:

在 android 下,您没有 main。您将需要 (1) 将该代码放在您需要的任何位置,并且您可能希望 ro (2) 异步执行。

在使用 kotlin 时,您还 (3) 需要相应地修改语法。

【讨论】:

  • 哦,谢谢,我一定会试试的
猜你喜欢
  • 2016-12-11
  • 1970-01-01
  • 2014-07-15
  • 2016-08-22
  • 2021-07-29
  • 2011-09-25
  • 2019-09-30
  • 2013-06-22
相关资源
最近更新 更多