如何使用YouTube数据API

问题描述 投票:0回答:2

我试着使用YouTube数据API.我真的好好看看我在互联网上找到的所有东西。代码本身不是问题,但我没有发现,在哪里使用这段代码。我是否简单地创建一个python文件(例如在Visual Studio Code中),然后在那里运行它?因为当我试着这样做的时候,它没有工作......我也看到很多人只用commander来使用API,其他人则在chrome中使用一些东西(localhost:8888......)。所以我真的不知道该怎么做,我应该怎么做。

谢谢你的帮助:)问候

api youtube youtube-api youtube-data-api
2个回答
0
投票

我不是一个python开发人员,但作为一个猜测,你可以从这里开始。

https:/developers.google.comyoutubev3quickstartpython。

使用pip来安装你所需要的依赖项。

你应该能够创建一个简单的python文件,通过API认证,然后调用google api客户端上的一个方法,然后输出它。这里有一些例子。

https:/github.comyoutubeapi-samplesblobmasterpython。


0
投票
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;

namespace Google.Apis.YouTube.Samples
{
  /// <summary>
  /// YouTube Data API v3 sample: upload a video.
  /// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
  /// See https://code.google.com/p/google-api-dotnet-client/wiki/GettingStarted
  /// </summary>
  internal class UploadVideo
  {
    [STAThread]
    static void Main(string[] args)
    {
      Console.WriteLine("YouTube Data API: Upload Video");
      Console.WriteLine("==============================");

      try
      {
        new UploadVideo().Run().Wait();
      }
      catch (AggregateException ex)
      {
        foreach (var e in ex.InnerExceptions)
        {
          Console.WriteLine("Error: " + e.Message);
        }
      }

      Console.WriteLine("Press any key to continue...");
      Console.ReadKey();
    }

    private async Task Run()
    {
      UserCredential credential;
      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 an application to upload files to the
            // authenticated user's YouTube channel, but doesn't allow other types of access.
            new[] { YouTubeService.Scope.YoutubeUpload },
            "user",
            CancellationToken.None
        );
      }


      var youtubeService = new YouTubeService(new BaseClientService.Initializer()
      {
        HttpClientInitializer = credential,
        ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
      });

      var video = new Video();
      video.Snippet = new VideoSnippet();
      video.Snippet.Title = "Default Video Title";
      video.Snippet.Description = "Default Video Description";
      video.Snippet.Tags = new string[] { "tag1", "tag2" };
      video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
      video.Status = new VideoStatus();
      video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
      var filePath = @"REPLACE_ME.mp4"; // Replace with path to actual movie file.

      using (var fileStream = new FileStream(filePath, FileMode.Open))
      {
        var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
        videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
        videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;

        await videosInsertRequest.UploadAsync();
      }
    }

    void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
    {
      switch (progress.Status)
      {
        case UploadStatus.Uploading:
          Console.WriteLine("{0} bytes sent.", progress.BytesSent);
          break;

        case UploadStatus.Failed:
          Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception);
          break;
      }
    }

    void videosInsertRequest_ResponseReceived(Video video)
    {
      Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);
    }
  }
}

0
投票
  1. 确保你的电脑上安装了python。
  2. 创建一个项目。谷歌的API和服务仪表板
  3. 启用Youtube v3 API。API库
  4. 创建凭证。全权证书向导
  5. 现在,你需要使用你创建的凭证来获得一个访问令牌和一个刷新令牌。
  6. 在以下库中找到一个验证示例。
  7. 复制并粘贴你从步骤4中得到的客户端ID和客户端秘密,并将它们粘贴到步骤6中找到的验证示例中(应该搜索OAuth2示例),这一步应该提供一个访问令牌和一个刷新令牌。
  8. 复制并粘贴一个Youtube上的例子。
  9. 用你得到的访问令牌和刷新令牌字段替换。

现在你应该可以通过输入任何终端来运行该文件。

python3 yourfile.py

[编辑]

API密钥与访问令牌不同。使用Google APIs进行身份验证主要有2种方式。

  1. 访问和刷新令牌
  2. API_KEY.

API密钥不能用于个人信息。你需要为此获得一个访问和刷新令牌(方法1)。

一旦你得到一个访问令牌,它的作用与你得到的API_KEY类似。获取访问令牌比只使用API_KEY要复杂一些。

刷新令牌是你在认证时与访问令牌一起获得的令牌。访问令牌在3600秒后失效。当它们过期时,你的身份验证库会用刷新令牌向Google的服务器索取一个新的访问令牌。刷新令牌的寿命非常长(通常是无限期的),所以请确保你安全地存储它。

要获得访问令牌和刷新令牌(用户凭证),你必须首先创建 客户端凭证. 其中应该包括:1.客户端ID和2.客户端秘密。这些只是普通的字符串。

你还应该在你的Google应用控制台中设置一个重定向URL,以便正确执行OAuth2流程。OAuth2流是许多API所依赖的认证协议,允许他们在用户同意的情况下对用户的账户进行操作。(例如,当一个应用程序要求你代为发帖或代为控制你的账户时,它通常会使用这个协议)。

Aiogoogle的文档在解释Google提供的认证流程方面做得不错。

https:/aiogoogle.readthedocs.ioenlatest。

但这是一个异步Python库。如果你对async语法不熟悉,可以先看一下文档,只是大致了解一下认证系统的工作原理,然后再应用到Google的同步Python库中。

关于第6点。我发布的链接,Aiogoogle是其中之一,只是帮助你更快、更少地访问Google的API的客户端库。这两个库都有文档,其中有如何使用它们的例子链接。所以,打开文档,阅读它,搜索发布的例子,试着理解例子中的代码如何工作。然后,也许下载它并在你自己的机器上运行它。

我建议你阅读 文件. 希望能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.