使用Java从我的Web服务器上传视频到Youtube [英] Upload videos to Youtube from my web server in Java

查看:234
本文介绍了使用Java从我的Web服务器上传视频到Youtube的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将上传到我的网络服务器的视频上传到Youtube 我自己的频道而不是用户的Youtube帐户(我的网络服务器充当代理商。

My goal is to upload videos that are uploaded to my web server to Youtube on my own channel, not the users' Youtube account (my web server is acting as a proxy).

我找到了将视频上传到Youtube的示例代码 here ,获取凭据这样。我对此示例的问题是它将凭据写入磁盘,并打开一个http服务器。由于我的Web服务器可能会有很多用户同时上传他们的视频,因此凭证文件位置必须是动态的,并且不可能多次绑定到同一个http端口。此外,在搜索了关于上传到Youtube的其他文章之后,我认为这种方法适用于上传到Youtube帐户的用户。

I found the sample code for uploading video to Youtube here with the credential acquired this way. The problem that I have with this sample is that it writes to disk the credential, and it opens an http server. Since my web server can potentially have a lot of users uploading their videos concurrently, the credential file location has to be dynamic, and multiple binding to the same http port is not possible. Further more, after searching through other writing about uploading to Youtube, I think this approach is for users uploading to their Youtube account.

您能否分享您的经验/代码示例/我的方案的解决方案?简而言之,我只是想尝试自动化我打开Youtube仪表板,并将视频上传到我的Youtube中的频道。

Could you share your experiences/code sample/solutions for my scenario? In short I am just trying to automate the process of me opening up Youtube dashboard, and uploading videos to a channel in my Youtube.

推荐答案

一般来说,从API V3开始,Google比其他机制更喜欢OAuth2,上传视频(或修改用户数据的任何其他操作)需要OAuth2。

In general, starting at API V3, Google prefers OAuth2 over other mechanism, and uploading a video (or any other action that modifies user data) requires OAuth2.

幸运的是,有一种特殊的令牌叫做刷新令牌来救援。刷新令牌不会像普通访问令牌一样过期,并在需要时用于生成普通访问令牌。因此,我将我的应用程序分为两部分:

Fortunately, there is a special kind of token called refresh token to the rescue. Refresh token does not expire like normal access token, and is used to generate normal access token when needed. So, I divided my application into 2 parts:


  • 第一部分用于生成刷新令牌,这是一个Java桌面应用程序,意味着由计算机上的用户运行。 请参阅此处,了解来自Google的示例代码。

  • 第二部分是我的网络应用程序的一部分,它使用给定的刷新令牌来创建凭证对象。

  • The 1st part is for generating refresh token, which is a Java desktop app, meant to be run by a user on a computer. See here for sample code from Google.
  • The 2nd part is is part of my web application, which uses a given refresh token to create a credential object.

这是我在Scala中的实现,您可以轻松地适应Java版本:

Here is my implementation in Scala, which you can adapt to Java version easily:

要生成刷新令牌,应将accessType设置为 offline 以获取授权流。注意:如果您的系统上已存在令牌,即使它没有刷新令牌,它也不会尝试获取新令牌,因此您还必须将审批提示设置为 force

For generating a refresh token, you should set the accessType to offline for the authorization flow. Note: if a token already exists on your system, it won't try to get new token, even if it does not have refresh token, so you also have to set approval prompt to force:

def authorize(dataStoreName: String, clientId: String, clientSecret: String): Credential = {

    val builder = new GoogleAuthorizationCodeFlow.Builder(
      HTTP_TRANSPORT,
      JSON_FACTORY,
      clientId,
      clientSecret,
      Seq(YouTubeScopes.YOUTUBE_UPLOAD)
    )

    val CREDENTIAL_DIRECTORY = s"${System.getProperty("user.home")}/.oauth-credentials"
    val fileDataStoreFactory = new FileDataStoreFactory(new java.io.File(CREDENTIAL_DIRECTORY))
    val dataStore: DataStore[StoredCredential] = fileDataStoreFactory.getDataStore(dataStoreName)

    builder.setCredentialDataStore(dataStore).setAccessType("offline").setApprovalPrompt("force")

    val flow = builder.build()

    val localReceiver = new LocalServerReceiver.Builder().setPort(8000).build()

    new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user")
}

val credential = authorize(dataStore, clientId, clientSecret)
val refreshToken = credential.getRefreshToken

要在服务器上使用刷新令牌,您可以从刷新令牌构建凭证:

For using the refresh token on the server, you can build a credential from a refresh token:

def getCredential = new GoogleCredential.Builder()
    .setJsonFactory(JSON_FACTORY)
    .setTransport(HTTP_TRANSPORT)
    .setClientSecrets(clientId, clientSecret)
    .build()
    .setRefreshToken(refreshToken)

这篇关于使用Java从我的Web服务器上传视频到Youtube的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆