youtube通过C#添加订阅

时间:2015-11-11 09:03:10

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

我正在为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;
}

1 个答案:

答案 0 :(得分:0)

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

$( document ).ready( function() {
  // init Isotope
  var $container = $('.isotope').isotope({
itemSelector: '.class-1' || '.class-2',
layoutMode: 'fitRows',
getSortData: {
  name: '.name',
  symbol: '.symbol',
  number: '.number parseInt',
  category: '[data-category]',
  weight: function( itemElem ) {
    var weight = $( itemElem ).find('.weight').text();
    return parseFloat( weight.replace( /[\(\)]/g, '') );
  }
}
  });


  // bind filter button click
  $('#filters').on( 'click', 'button', function() {
var filterValue = $( this ).attr('data-filter');
// use filterFn if matches value
filterValue = filterFns[ filterValue ] || filterValue;
$container.isotope({ filter: filterValue });
  });

  // bind select option value
  $('#filters').on( 'change', 'select', function() {
var filterValue = $( this ).attr('data-filter');
// use filterFn if matches value
filterValue = filterFns[ filterValue ] || filterValue;
$container.isotope({ filter: filterValue });
      });

  // change is-checked class on buttons
  $('.button-group').each( function( i, buttonGroup ) {
var $buttonGroup = $( buttonGroup );
$buttonGroup.on( 'click', 'button', function() {
  $buttonGroup.find('.is-checked').removeClass('is-checked');
  $( this ).addClass('is-checked');
   });
  });

});