使用API​​的v3检索Objective-C中的YouTube频道上传

时间:2012-12-11 16:11:15

标签: 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 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);
                }];

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

  

错误Domain = com.google.GTLJSONRPCErrorDomain Code = -32602“The   操作无法完成。 (RayWilliamJohnson)”   UserInfo = 0x7532c40 {error = RayWilliamJohnson,   GTLStructuredError = GTLErrorObject 0x7533d30:   {message:“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);
            }];

但我仍然收到“未指定过滤器”的错误,这意味着变量未设置为......

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”类:“youtube#searchListResponse”项目:[1]}

感谢您的帮助Jeff

3 个答案:

答案 0 :(得分:2)

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

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

一个必需参数是part,我假设这是ueryForChannelsListWithPart()方法作为其一个参数所接受的内容。 part应设置为idid,contentDetails,如文档中所述。

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

该API的第3版不支持在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中没有端到端的例子。

答案 1 :(得分:2)

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");
    }
}];

`

答案 2 :(得分:1)

有关如何按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);
    }
}];