使用API的v3在Objective-C中检索YouTube频道上载

使用API的v3在Objective-C中检索YouTube频道上载,objective-c,youtube-api,Objective C,Youtube Api,您好,我正在尝试使用xcode中的google api库从youtube api获取频道对象,这是我正在使用的代码: //youtubeService type is GTLServiceYouTube youtubeService = [[GTLServiceYouTube alloc] init]; youtubeService.shouldFetchNextPages = YES; //auth is the object from the authenti

您好,我正在尝试使用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);
                }];
到目前为止,我一直遇到这个错误:

    //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“该 无法完成操作。(RayWilliamJohnson) UserInfo=0x7532c40{error=RayWilliamJohnson, 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.)}
Edit3: 好的,我已经改变了两件事,首先我犯了一个愚蠢的错误,在设置参数后初始化查询,而参数只是删除了它们。因此代码现在看起来如下:

        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);
            }];
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);
            }];
但我仍然得到了“未指定过滤器”的错误,这意味着变量不会以任何方式设置

Edit4:好了,在我尝试了这么多金发女郎的时刻之后,我正在初始化查询对象,我试图使用前缀对象,它是专门为频道查找而设计的,我应该只使用queryForSearchListWithPart,因此工作的代码如下所示:

        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);
            }];
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](空)2012-12-12 00:17:21.316测试[13099:c07]获胜!:GTLYOUTUBESEARCHLIST响应 0x727f6d0:{pageInfo:{totalResults,resultsPerPage} etag:“bm4jokyoi0qsqrxeni8h7sne4/A2tE7ag9HxUC5HxeD2vDlGNg5iM” 下一个GetOken:“CAEQAA”类:“youtube#searchListResponse”项:[1]}


谢谢你的帮助Jeff

我不是Objective-C程序员,因此我无法具体建议如何修改代码,但这里是一般问题

看起来您正在调用YouTube数据API v3
channels.list()
方法,该方法记录在

一个必需的参数是
part
,我假设
ueryForChannelsListWithPart()
方法将其作为一个参数。
part
应设置为类似
id
id,contentDetails
,如文档中所述

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

API的v3不支持在
channels.list()
调用中指定旧版YouTube用户名。它确实接受
id
参数,该参数对应于频道的YouTube频道id您需要检索有关的信息。这些信息通常是
UC…
,在
RayWilliamJohnson
的情况下,通道id是
UCGt7X90Au6BV8rf49BiM6Dg
(您可以通过
search.list()
调用来解决这个问题;下面是一个示例。)


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

任何搜索如何通过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);
    }
}];
GTLServiceYouTube*youTubeService=[[GTLServiceYouTube alloc]init];
youTubeService.APIKey=@;
GTLQueryYouTube*channelsQuery=[GTLQueryYouTube QUERYFORCHANNELSLIST WITHPART:@“id、代码段、品牌设置、内容详细信息、INVIDEO促销、统计信息、状态、主题详细信息”];
channelsQuery.identifier=@”;
[youTubeService executeQuery:channelsQuery completionHandler:^(GTLServiceTicket*票证,GTLYouTubeChannelListResponse*channelListResponse,NSError*错误){
如果(错误==nil){
如果(channelListResponse.items.count>0){
GTLYouTubeChannel*channel=channelListResponse.items.firstObject;
NSLog(@“获得youtube频道-%@”,频道);
}否则{
NSLog(@“找不到id为%@”的频道),channelsQuery.identifier;
}
}否则{
NSLog(@“无法获取youtube频道-%@”,错误);
}
}];

`

我现在就尝试一下,如果我成功了,我会更新线程。我已经尝试过按照您所说的设置参数,但这对我不起作用,我得到了相同的错误代码,即“-32602”,很高兴听到您现在可以工作了!我们真的需要一些“官方”消息Objective-C示例代码。现在在图书馆的存储库中有一个用于YouTube的Objective-C示例:我很高兴我不必再使用ObjC了。谢谢你的回答,尽管它也可以用于检索用户的频道-只需将“channelsQuery.identifier=@;”替换为“channelsQuery.mine=YES;”你好@PulKit Goyal,我也试过了,但是我得到了错误“没有选择过滤器”。你知道为什么需要你的答案吗?我现在已经转到Swift了