【问题标题】:Exception when trying to fetch channelId using the youtube api in objective c尝试在目标 c 中使用 youtube api 获取 channelId 时出现异常
【发布时间】:2013-03-27 14:51:48
【问题描述】:

我正在尝试使用 google-api-objectivec-client 获取 youtube 频道 ID。我遇到的问题基本上是由于某种原因我在尝试访问 channelId 时收到异常。我正在使用的代码:

GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = _MY_API_KEY_;
GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"id"];
query.q = @"google";
query.type = @"channel";
query.maxResults = 1;
GTLServiceTicket *ticket = [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
    if (error == nil) {
        GTLYouTubeSearchListResponse *products = object;
        for (id item in products.items) {
            GTLYouTubeSearchResult *result = item;
            NSLog(@"Identifier:%@",result.identifier);
            GTLYouTubeResourceId* resourceId = result.identifier;
            NSLog(@"kind:%@",resourceId.kind);
            NSLog(@"channel:%@",resourceId.channelId);
        }
    }else{
        NSLog(@"Error: %@", error.description);
    }
}];

我在运行这段代码时得到的输出是:

2013-04-05 11:37:12.615 YouTest[21704:11303] Identifier:GTLYouTubeChannel 0x7233b00: {kind:"youtube#channel" channelId?:"UCK8sQmJBp8GCxrOtXWBpyEA"}
2013-04-05 11:37:12.617 YouTest[21704:11303] kind:youtube#channel
2013-04-05 11:37:12.617 YouTest[21704:11303] -[GTLYouTubeChannel channelId]: unrecognized selector sent to instance 0x7233b00
2013-04-05 11:37:12.618 YouTest[21704:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GTLYouTubeChannel channelId]: unrecognized selector sent to instance 0x7233b00'

因此,当我尝试访问 resourceId 的 channelId 时,我的实现崩溃了。从文档中我了解到 channelId 应该在那里,因为 resourceId 的类型是 youtube#channel。 channelId 可以从我也在打印的 result.identifier 字符串中解析出来,但是由于 channelId 有一个属性,我更喜欢使用它。

对我的代码有什么问题有任何想法吗?

【问题讨论】:

    标签: objective-c youtube-api


    【解决方案1】:

    Google 库中确实存在错误。但是我通过直接访问 JSON 字符串并在 NSString+SBJSON.h 类的帮助下解析它解决了这个问题,如本例所示。

    #import "NSString+SBJSON.h"
    
    ...
    
    GTLYouTubeResourceId *resource = channel.snippet.resourceId;
    
    NSDictionary *jsonObject = [resource.JSONString JSONValue];
    
    NSString *channelid = [jsonObject valueForKey:@"channelId"];
    

    【讨论】:

      【解决方案2】:

      我对 Objective-C 不是很熟悉,但是,是的,生成的客户端库的 YouTube 数据 API v3 绑定似乎有问题。您使用的是project page 的最新版本吗?如果您可以使用最新版本重现客户端库,您可能需要file a bug。在进一步解决此问题时,我会检查您在query.type = @"video"; 时是否有同样的问题,并且您尝试访问响应项的 videoId。

      不过,您可以尝试以下替代方案。频道的 id 也会在 snippet.channelId 属性中返回。如果您通过GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"snippet"]; 请求 sn-p 部分,请查看是否可以改为读取该值。

      【讨论】:

        【解决方案3】:

        我有同样的问题。用下面的方法解决了...

             NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:[resourceId.JSONString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
                    NSString *channelId = [jsonObject valueForKey:@"channelId"];
                    NSLog(@"channelId is %@", channelId);
        

        【讨论】:

          【解决方案4】:

          解决方法代码:

              channel.snippet.resourceId.JSON[@"channelId"];
          

          由于底层 JSON 已暴露,因此无需自己解析 JSON。

          自动绑定似乎不适用于 GTLYouTubeResourceId,因为“youtube#channel”的“kind”元素正在放弃运行时对象创建并改为创建 GTLYouTubeChannel。

          彻底的解决方法代码:

          ticket.surrogates = @{ (id)[GTLYouTubeChannel class] : [GTLYouTubeResourceId class] };
          

          如果您真的想强制该绑定工作,您可以在执行查询时在票证上更进一步的上游解决方法。

          全局变通补丁:

          https://github.com/google/google-api-objectivec-client/pull/109

          该问题有待售票:

          https://github.com/google/google-api-objectivec-client/issues/63

          https://github.com/google/google-api-objectivec-client/issues/92

          似乎他们想更改 API 以不调用 resourceId.kind 'kind' 以避免此问题。但是,当我们等待 API 更改时,这三种变通方法中的任何一种都应该满足您的目的。

          【讨论】:

            猜你喜欢
            • 2011-11-24
            • 2016-12-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-09-13
            相关资源
            最近更新 更多