.NET - 如何使用 OAuth2.0 和从 Windows 服务离线访问将视频上传到 YouTube

use*_*059 5 c# google-api google-oauth google-api-dotnet-client youtube-data-api

我正在使用从 Nuget 下载的 Youtube .Net 库。

我创建了一个 Windows 服务,它正在检查一个文件夹以将新视频上传到 youtube。我使用控制台应用程序进行了测试,对于这种情况,用户必须在网络浏览器上手动进行授权,之后我可以毫无问题地将视频上传到 youtube。问题是当我尝试使用离线访问时。

当我在 Windows 服务上运行我的代码时,我无法从用户那里获得手动授权,所以我按照这个答案创建了我的访问令牌:https : //stackoverflow.com/a/20689423/2277059

问题是我没有找到任何使用独立版本的代码示例,所以我有点迷茫。使用我当前的代码,我在尝试上传视频时总是遇到错误:“unauthorized_client”。您可以在我的代码“client_secrets.json”中看到的 JSON 文件是您在为 YouTube API V3 创建凭据时自动生成的文件。

我也试过这个:https : //stackoverflow.com/a/43895931/2277059,但得到了同样的错误。

现在我没有从代码中刷新令牌,只是在 OAuth Playground 上进行并测试服务。一旦成功,我将研究如何在每次查询时刷新令牌。

创建凭据时,我选择了“其他”类型,因为这是 Windows 服务。

是我的代码有问题还是我在配置中遗漏了什么?

这是我的代码:

var token = new TokenResponse()
{
    AccessToken = "werwdsgfdg...",
    ExpiresInSeconds = 3600,
    RefreshToken = "3/dsdfsf...",
    TokenType = "Bearer"
};

Log.Info("Generating user credentials and secrets");

UserCredential credential;
string credentialsPath = System.AppDomain.CurrentDomain.BaseDirectory + "client_secrets.json";

using (var stream = new FileStream(credentialsPath, FileMode.Open, FileAccess.Read))
{
    credential = new UserCredential(new GoogleAuthorizationCodeFlow(
    new GoogleAuthorizationCodeFlow.Initializer
    {
        ClientSecrets = GoogleClientSecrets.Load(stream).Secrets,
        Scopes = new[] { YouTubeService.Scope.YoutubeUpload }
    }
    ), EsSettings.YoutubeUser, token);
}


Log.Info("Generating youtube service");
//GoogleAuthorizationCodeFlow
YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});


Log.Info("Uploading...");
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();
}
Run Code Online (Sandbox Code Playgroud)

DaI*_*mTo 3

\n

错误:“unauthorized_client”

\n
\n\n

意味着您发送的客户端 ID 尚未经过用户身份验证。

\n\n

你得到这个的原因是因为你正在这样做

\n\n
\n

现在我不会从代码中刷新令牌,只是在 OAuth Playground 上执行它并测试服务。一旦它起作用,我将研究如何在每个查询中刷新令牌。

\n
\n\n

令牌与客户端相关联。您的 client_secrets.json 文件包含您的客户端,但您从 oauth Playground 获取的令牌与该客户端无关。

\n\n

您应该在自己的机器上运行一次代码来验证它。检查可在您的文件夹中找到的凭据%appData%文件中的访问令牌和刷新令牌,然后将它们发送到您的令牌。

\n\n

这是文件应包含的内容。

\n\n
{ \xef\x85\x87 \n   "access_token":"ya29.4wEk2VsbqiPfR4oH5WaYo7aYgAmlP2KSIl-heyDnPBBHMYYKnfU6YuQ-_RsDofD8QR1T",\n   "token_type":"Bearer",\n   "expires_in":3600,\n   "refresh_token":"1/PSvxzxGB-3XU8bF2SrG6llzO-ZizE4mftrd9Edqbubg",\n   "Issued":"2015-09-03T11:43:47.681+02:00"\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

您可以在此处了解如何创建文件FileDatastore

\n

  • 会自动刷新!有时,我等了几天才重新使用令牌,并且它总是自动刷新。请参阅 [https://github.com/google/google-api-dotnet-client-samples][1] 上的 ResumeableUpload 示例。[1]:https://github.com/google/google-api-dotnet-client-samples (2认同)