4

您好,我正在尝试使用 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 代码 = -32602“操作无法完成。(RayWilliamJohnson)”用户信息 = 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);
            }];

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

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]}

感谢您的帮助杰夫

4

3 回答 3

2

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

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

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

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

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

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

于 2012-12-11T22:08:15.940 回答
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");
    }
}];

`

于 2015-06-23T13:13:39.283 回答
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);
    }
}];
于 2014-06-15T14:10:08.427 回答