【问题标题】:youtube Add Subscription via C#youtube 通过 C# 添加订阅
【发布时间】:2016-02-12 08:18:48
【问题描述】:

我正在为 Windows 8.1 构建 YouTube 应用。

我遇到了问题,添加(插入)订阅失败

我的代码:

var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                new Uri("ms-appx:///Assets/client_secrets.json"),
                new[] { Uri.EscapeUriString(YouTubeService.Scope.Youtube) },
                "user",
                CancellationToken.None);

var youtubeService = new YouTubeService(new BaseClientService.Initializer()
                {
                    ApiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
                    HttpClientInitializer = credential,
                    ApplicationName = "AppName"
                });

Subscription body = new Subscription();
body.Snippet = new SubscriptionSnippet();

body.Snippet.ChannelId = "UC-kezFAw46x-9ctBUqVe86Q"; 
try
{
    var addSubscriptionRequest = youtubeService.Subscriptions.Insert(body, "snippet");
    var addSubscriptionResponse = await addSubscriptionRequest.ExecuteAsync();
}
catch (Exception e)
{
    throw e;
}

当我在第一行设置断点时。

当执行到最后一行时,中断这个函数

更新(2015-11-14):

错误信息: 请求中指定的订阅资源必须使用 snippet.resorceId 属性来标识正在订阅的频道[400]

成功的代码:

var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
         new Uri("ms-appx:///Assets/client_secrets.json"),
         new[] { Uri.EscapeUriString(YouTubeService.Scope.Youtube) },
         "user",
         CancellationToken.None);

var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
    //ApiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    HttpClientInitializer = credential,
    ApplicationName = "4GameTV"
});


try
{
    Subscription body = new Subscription();
    body.Snippet = new SubscriptionSnippet();
    body.Snippet.ResourceId = new ResourceId();
    body.Snippet.ResourceId.ChannelId = "UC-kezFAw46x-9ctBUqVe86Q";  //replace with specified channel id

    var addSubscriptionRequest = youtubeService.Subscriptions.Insert(body, "snippet");
    var addSubscriptionResponse = await addSubscriptionRequest.ExecuteAsync();

}
catch (Exception e)
{
    throw e;
}

【问题讨论】:

    标签: c# youtube-api google-oauth google-api-dotnet-client


    【解决方案1】:

    您的身份验证似乎与我通常使用的有所不同。此外,仅当您想访问公共数据时才需要 ApiKey。

    string[] scopes = new string[] { YouTubeService.Scope.Youtube };
    
    
     // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
    UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
    , scopes
    , "test"
    , CancellationToken.None
    , new FileDataStore("Daimto.YouTube.Auth.Store")).Result;
    
    YouTubeService service = new YouTubeService(new YouTubeService.Initializer()
         {
         HttpClientInitializer = credential,
         ApplicationName = "YouTube Data API Sample",
         });
    

    【讨论】:

    • 什么是 FileDataStore?这是一堂课吗?
    • 实现 IDataStore 的文件数据存储。此存储为类型和密钥的每种组合创建一个不同的文件。此文件数据存储存储指定对象的 JSON 格式。它会保存您的身份验证,因此您不会被要求再次进行身份验证。刷新令牌所在的位置。
    • 听起来好难=(,能不能提供一些网页让我明白你说的,谢谢。
    • 我不久前写了一篇关于它的教程daimto.com/google-net-filedatastore-demystified 示例适用于 Google 驱动器,但您应该明白。
    • 感谢您的回复。我查过那个网站。好像在教如何恢复、刷新token。我的令牌永久存在,所以我认为这不是问题。我这样说是因为我的原始代码适用于 [删除订阅] 功能。还是其他代码有问题?
    猜你喜欢
    • 2018-10-20
    • 1970-01-01
    • 2016-06-12
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 2011-06-12
    相关资源
    最近更新 更多