【问题标题】:How can I upload a videofile under my youtube/google account while using a service account如何在使用服务帐户时在我的 youtube/google 帐户下上传视频文件
【发布时间】:2014-05-02 15:16:59
【问题描述】:

我希望创建一个服务器应用程序来检查所述服务器上的文件夹并自动上传已放入所述文件夹的所有视频文件。服务器是远程的,不会发生任何人为交互。

在阅读了 Youtube 的 API v3 指南后,我得出的结论是,我需要一个“服务帐户”来上传这些视频,因为应用程序会持续监控这个文件夹。我的问题是:是否有可能使用服务帐户上传视频,然后将视频放在我的个人 youtube 帐户下?

如果这实际上是可能的;我创建了以下代码,但遇到了 401 Unauthorized (Invalid Token) 错误。

我有以下可编译源;我试图让它尽可能小:

package PACKAGE;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
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.HttpTransport;
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.YouTubeScopes;
import com.google.api.services.youtube.model.PlaylistItem;
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.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

public class BigQueryTestApp {

  private static final String VIDEO_FILE_FORMAT = "video/*";
  private static final String SAMPLE_VIDEO_FILENAME = "sample_video.mp4";
  public static void main(String[] args) throws IOException, InterruptedException, GeneralSecurityException {

    final HttpTransport TRANSPORT = new NetHttpTransport();
    final JsonFactory JSON_FACTORY = new JacksonFactory();
    File privateKeyFile = new File("privatekey.p12");

    GoogleCredential credential = new  GoogleCredential.Builder()
      .setTransport(TRANSPORT)
      .setJsonFactory(JSON_FACTORY)
      .setServiceAccountId("XXXX@developer.gserviceaccount.com")
      .setServiceAccountScopes(YouTubeScopes.all())
      .setServiceAccountPrivateKeyFromP12File(privateKeyFile)
      .build();


    try
    {

      YouTube youtube = new YouTube.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("CHANGE_IT").build();
      Video videoObjectDefiningMetadata = new Video();

      VideoStatus status = new VideoStatus();
      status.setPrivacyStatus("unlisted");
      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("example");
      tags.add("java");
      tags.add("YouTube Data API V3");
      tags.add("erase me");
      snippet.setTags(tags);

      videoObjectDefiningMetadata.setSnippet(snippet);

      InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT,
              BigQueryTestApp.class.getResourceAsStream(SAMPLE_VIDEO_FILENAME));
      YouTube.Videos.Insert videoInsert = youtube.videos().insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);
      MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();//thumbnailSet.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 (GoogleJsonResponseException e) {

            System.out.println(e.getMessage());
            System.out.println("Headers: "+e.getHeaders().toString());
    }

最后,我阅读了有关服务帐户的各种故事。有人说它们与 Youtube 兼容,其他人则说它们不兼容。这让我感到困惑,我在 stackoverflow 上发现了更多问题,这些问题并不能明确说明它是否可能。

【问题讨论】:

    标签: java youtube-api google-oauth


    【解决方案1】:

    YouTube 不支持服务帐户,他们也没有计划:

    https://code.google.com/p/gdata-issues/issues/detail?id=5370

    那么,您的一个选择是设置您的应用以执行初始 oAuth 流程(您通过用户交互启动一次),然后作为该流程的一部分,您将获得临时访问权限令牌和刷新令牌。刷新令牌可用于获取更多访问令牌,而无需再次运行 oAuth 流程,并且刷新令牌不会自行过期(但可以手动撤销)。因此,您的应用可以存储您的刷新令牌,监控您的文件夹,然后在需要上传新视频时自行获取访问令牌。请参阅此链接:

    https://developers.google.com/youtube/v3/guides/moving_to_oauth#standalone

    (请注意,它特别提到了一个守护进程的想法,该守护进程监控要上传的新视频的目录)。

    【讨论】:

      猜你喜欢
      • 2019-01-11
      • 1970-01-01
      • 2020-11-30
      • 2020-07-13
      • 2011-05-14
      • 2015-08-28
      • 2016-06-05
      • 2013-08-09
      • 1970-01-01
      相关资源
      最近更新 更多