如何使用已生成的令牌上传YouTube视频?

时间:2018-10-16 07:26:19

标签: c# asp.net-mvc google-api youtube-api google-api-dotnet-client

我正在引用youtube上传文档

https://developers.google.com/youtube/v3/code_samples/dotnet

为上传YouTube视频而指定的链接,希望它具有youube客户ID和所有ID,但我已使用以下方法创建了有效令牌。

public ActionResult Login()
        {
            string clientId = ConfigurationManager.AppSettings["v.YouTube.ClientId"];
            string redirect = HttpUtility.UrlEncode(ConfigurationManager.AppSettings["x.YouTube.CallbackUrl"]);
            string scope = ConfigurationManager.AppSettings["x.YouTube.Scopes"];

            return Redirect($"https://accounts.google.com/o/oauth2/auth?client_id={clientId}&redirect_uri={redirect}&scope={scope}&response_type=code&access_type=offline");
        }

现在我有一个有效的令牌,如何在不使用

的情况下上传视频
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
  {
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        // This OAuth 2.0 access scope allows for full read/write access to the
        // authenticated user's account.
        new[] { YouTubeService.Scope.Youtube },
        "user",
        CancellationToken.None,
        new FileDataStore(this.GetType().ToString())
    );
  }

上述方法。

有什么办法吗?

1 个答案:

答案 0 :(得分:1)

如果您已经具有访问令牌,则可以使用GoogleCredential.FromAccessToken(...)方法创建一个GoogleCredential实例。

请注意,以这种方式创建GoogleCredential意味着它无法自动刷新访问令牌。因此它将在大约一小时后失效。

然后,您可以使用此GoogleCredential实例,使用@DalmTo所示的代码将其上传到YouTube