【问题标题】:Retrieving YouTube Channel Uploads in Objective-C using v3 of the API使用 API v3 在 Objective-C 中检索 YouTube 频道上传
【发布时间】:2012-11-29 05:20:41
【问题描述】:

您好,我正在尝试使用 xcode 中的 google-api 库从 youtube api 获取频道对象,这是我正在使用的代码:

    //youtubeService type is GTLServiceYouTube
    youtubeService = [[GTLServiceYouTube alloc] init];
    youtubeService.shouldFetchNextPages = YES;
    //auth is the object from the authentication callback 
    youtubeService.authorizer = auth;
    videoQuery = [GTLQueryYouTube 
    queryForChannelsListWithPart:@"RayWilliamJohnson"];
    videoQuery.maxResults = 20;

    youtubeTicket = [youtubeService executeQuery:videoQuery completionHandler:
^(GTLServiceTicket *ticket, id channel, NSError *error) {
                NSLog(@" %@ ", error.description);
                NSLog(@"WIN! : %@ ", channel);
                }];

到目前为止,我一直收到此错误:

错误域=com.google.GTLJSONRPCErrorDomain Code=-32602 "的 操作无法完成。 (雷威廉约翰逊)” 用户信息=0x7532c40 {错误=雷威廉约翰逊, GTLStructuredError=GTLErrorObject 0x7533d30: {消息:“RayWilliamJohnson”代码:-32602 数据:[1]}, NSLocalizedFailureReason=(RayWilliamJohnson)}

知道是什么引发了这个错误吗?

提前感谢您的帮助!

编辑: 我已经阅读了“不存在”的类中的文档:P 并检查了也遵循相同方法的其他示例。

Edit2:好的,我尝试了以下更改,但没有一个起作用 :(,它在查询资源管理器上使用相同的示例对我有用。

videoQuery.maxResults = 20;
videoQuery.q = @"RayWilliamJohnson";
videoQuery.type = @"channel";

/*tried setting the part without calling the query 
 method on GTLQueryYoutube but that did not work videoQuery.part = @"id";*/

videoQuery = [GTLQueryYouTube queryForChannelsListWithPart:@"id"];
//fetching only 20 items.
youtubeTicket = [youtubeService executeQuery:videoQuery completionHandler:
^(GTLServiceTicket *ticket, id channel, NSError *error) {
    NSLog(@" %@ ", error.description);
    NSLog(@"WIN! : %@ ", channel);
    }];

错误略有不同,它表示我没有指定任何过滤器,即使我指定了。

Error Domain=com.google.GTLJSONRPCErrorDomain Code=-32602 "The operation couldn’t be completed. (No filter selected.)" UserInfo=0x8870250 {error=No filter selected., GTLStructuredError=GTLErrorObject 0x88707f0: {message:"No filter selected." code:-32602 data:[1]}, NSLocalizedFailureReason=(No filter selected.)}

编辑3: 好的,我已经改变了几件事,首先我犯了一个愚蠢的错误,我在设置参数后初始化我的查询,这只是删除了它们。所以代码现在看起来像这样:

        youtubeService.authorizer = auth;
        videoQuery = [GTLQueryYouTube queryForChannelsListWithPart:@"id"];
        videoQuery.maxResults = 1;
        videoQuery.q = @"RayWilliamJohnson";
        videoQuery.type = @"channel";


        youtubeTicket = [youtubeService executeQuery:videoQuery
                         completionHandler:^(GTLServiceTicket *ticket, GTLYouTubeChannel *channel,
                                             NSError *error) {
            NSLog(@" %@ ", error.description);
            NSLog(@"WIN! : %@ ", channel.description);
            }];

但我仍然收到“未指定过滤器”的错误,这意味着变量未设置任何一种方式...

Edit4:好的,经过这么多金发女郎的时刻,我设法做到了,这是我正在初始化的查询对象,我试图使用专门用于频道查找的前缀对象,我'应该只使用 queryForSearchListWithPart,所以有效的代码如下所示:

youtubeService.authorizer = auth;       
videoQuery = [GTLQueryYouTube queryForSearchListWithPart:@"id"];
videoQuery.maxResults = 1;
videoQuery.q = @"RayWilliamJohnson";      
youtubeTicket = [youtubeService executeQuery:videoQuery
         completionHandler:^(GTLServiceTicket *ticket, GTLYouTubeChannel *channel,
            NSError *error) {
            NSLog(@" %@ ", error.description);
            NSLog(@"WIN! : %@ ", channel.description);
            }];

并提供了这个结果:

2012-12-12 00:17:21.315 测试[13099:c07] (null) 2012-12-12 00:17:21.316 测试[13099:c07] 赢了! : GTLYouTubeSearchListResponse 0x727f6d0: {pageInfo:{totalResults,resultsPerPage} etag:""bm4JOKyioI0quSqrxEnI8H7snE4/A2tE7ag9HxUC5HxeD2vDlGNg5iM"" nextPageToken:"CAEQAA" kind:"youtube#searchListResponse" items:[1]}

感谢您的帮助杰夫

【问题讨论】:

标签: objective-c youtube-api


【解决方案1】:
service = [[GTLServiceYouTube alloc] init];
service.shouldFetchNextPages = YES;
service.retryEnabled = YES;
service.APIKey = @"xxxxxxxx";

GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"id,snippet"];
query.maxResults = 15;
query.q = YOUR_SEARCH_TEXT;
query.fields = @"items(id,snippet)";
query.order = @"viewCount";

[service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
    if (!error) {
        for (GTLYouTubeVideoListResponse *videoInfo in object) {
            [youtubeList addObject:videoInfo.JSON];
        }
        NSLog(@"youtubeList count %lu",(unsigned long)youtubeList.count);
        NSLog(@"youtubeList %@",youtubeList);
        [self.tableView reloadData];
    } else {
        NSLog(@"%@", error);
    }
    -- i'm test this currently for pagination --
    GTLYouTubePlaylistItemListResponse *playlistItems = object;
    NSLog(@"playlistItems.nextPageToken %@",playlistItems.nextPageToken);
    NSLog(@"playlistItems.prevPageToken %@",playlistItems.prevPageToken);
    if(playlistItems.nextPageToken) {
        NSLog(@"new page!!!");
        query.pageToken = playlistItems.nextPageToken;
        [self requestYoutubeVideo:withText];
    } else {
        NSLog(@"No more pages");
    }
}];

`

【讨论】:

  • 感谢您的回答,我现在已经转向 Swift ?
【解决方案2】:

任何搜索如何通过 id 查找频道的人,使用此

GTLServiceYouTube *youTubeService = [[GTLServiceYouTube alloc] init];
youTubeService.APIKey = @"<YOUR_API_KEY>";

GTLQueryYouTube *channelsQuery = [GTLQueryYouTube queryForChannelsListWithPart:@"id,snippet,brandingSettings,contentDetails,invideoPromotion,statistics,status,topicDetails"];
channelsQuery.identifier = @"<CHANNEL_ID>";

[youTubeService executeQuery:channelsQuery completionHandler:^(GTLServiceTicket *ticket, GTLYouTubeChannelListResponse *channelListResponse, NSError *error) {
    if (error == nil) {
        if (channelListResponse.items.count > 0) {
            GTLYouTubeChannel *channel = channelListResponse.items.firstObject;
            NSLog(@"Got youtube channel - %@", channel);
        } else {
            NSLog(@"Cannot find channels with id %@", channelsQuery.identifier);
        }
    } else {
        NSLog(@"Cannot get youtube channels - %@", error);
    }
}];

【讨论】:

  • 我很高兴我不必再使用 ObjC 了,不过感谢您的回答
  • 也可用于检索用户的频道 - 只需替换 "channelsQuery.identifier = @"";"与“channelsQuery.mine = YES;”
  • 嗨@PulKit Goyal,我尝试与您相同,但出现错误“未选择过滤器”。你知道为什么
【解决方案3】:

我不是 Objective-C 程序员,所以我无法具体建议如何修改您的代码,但这是一般问题。

您似乎正在调用 YouTube 数据 API v3 channels.list() 方法,该方法记录在 https://developers.google.com/youtube/v3/docs/channels/list

一个必需的参数是part,我认为这是ueryForChannelsListWithPart() 方法的一个参数。 part 应设置为 idid,contentDetails,如文档中所述。

然后您需要添加一个额外的参数来告诉 API 您正在尝试检索有关哪个通道的信息。文档的“过滤器”部分列出了各种选项。

API 的 v3 不支持在 channels.list() 调用中指定旧版 YouTube 用户名。它确实接受 id 参数,该参数对应于您要检索其信息的频道的 YouTube 频道 ID。这些通常是UC... 的形式,在RayWilliamJohnson 的情况下,频道ID 是UCGt7X90Au6BV8rf49BiM6Dg。 (您可以通过search.list() 调用解决此问题;这里是example using the API Explorer。)

请注意,channels.list() 方法实际上并不返回视频列表。如果要检索给定频道中的最新视频,则需要调用channels.list() 以从contentDetails 部分获取相应的上传播放列表ID,然后调用playlistItems.list() 以获取视频上传播放列表。我们在 Python 中有一个这样做的例子,但在 Objective-C 中还没有端到端的例子。

【讨论】:

  • 我现在试一试,如果我成功了就更新线程
  • 我已经尝试按照您的说法设置参数,但这对我不起作用,我得到了相同的错误代码,即“-32602”
猜你喜欢
  • 2015-01-02
  • 2016-03-12
  • 2015-06-27
  • 2015-07-22
  • 2016-10-11
  • 2014-12-30
  • 2018-02-06
  • 2021-01-15
  • 2016-08-30
相关资源
最近更新 更多