【问题标题】:How to get YouTube channel name?如何获取 YouTube 频道名称?
【发布时间】:2013-07-22 07:57:02
【问题描述】:

我在YouTube Documents 中进行了搜索,但没有找到任何可以从 YouTube 视频中获取其他频道名称的内容。

也就是说,

我目前想从video获取频道名称,我只有网址,如何获取频道名称?

【问题讨论】:

    标签: youtube youtube-api youtube-channels


    【解决方案1】:

    您可以从 URL 中提取视频 id,然后发出 HTTP GET 请求:

    https://gdata.youtube.com/feeds/api/videos/dQw4w9WgXcQ?v=2&alt=json

    其中dQw4w9WgXcQ 是您感兴趣的视频ID。这将返回一个JSON 响应,频道名称位于author 字段中(点击链接查看示例)。

    有关详细信息,请参阅 Retrieving Data for a Single Video 和 YouTube API 文档的其余部分。

    参见例如How do I find all YouTube video ids in a string using a regex? 了解从 YouTube URL 获取视频 ID 的一些方法。

    【讨论】:

    • +1 帮助我解决了我自己的类似问题。有用。我不敢相信 youtube API-3 没有更简单的方法但是很好......它可以工作......
    【解决方案2】:

    您可以使用YouTube Data API v3 轻松做到这一点。

    URL 中“http://www.youtube.com/watch?v=”之后的最新部分是您的 VIDEO_ID。

    只需设置part="sn-p" 即可videos->list。然后你可以在响应中获取snippet.channelId

    请求将是:

    GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=EhNWzcUqGbI&key={YOUR_API_KEY}

    你的例子。

    您可以随时使用API explorer 尝试这些。

    Great sample codes 让您开始。

    【讨论】:

      【解决方案3】:

      对于迷路的新手:考虑一个示例函数,该函数将有助于了解获取、解析、显示等的整个周期,并将 youtube 频道的视频专门带到您的 tableview 中。我没有在这里写tableview部分

          -(void)initiateRequestToYoutubeApiAndGetChannelInfo
      {
      NSString * urlYouCanUseAsSample = @"https://www.googleapis.com/youtube/v3/search?key={YOUR_API_KEY_WITHOUT_CURLY_BRACES}&channelId={CHANNEL_ID_YOU_CAN_GET_FROM_ADDRESS_BAR_WITHOUT_CURLY_BRACES}&part=snippet,id&order=date&maxResults=20";
      
      
      
      NSURL *url = [[NSURL alloc] initWithString: urlYouCanUseAsSample];
      
      // Create your request
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      
      
      
        // Send the request asynchronously remember to reload tableview on global thread
      [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
      
          // Callback, parse the data and check for errors
          if (data && !connectionError) {
              NSError *jsonError;
      
              NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
      
              if (!jsonError) {
              // better put a breakpoint here to see what is the result and how it is brought to you. Channel id name etc info should be there
      
                  NSLog(@"%@",jsonResult);
      
              /// separating "items" dictionary and making array
      
                  // 
          id keyValuePairDict = jsonResult;
          NSMutableArray * itemList = keyValuePairDict[@"items"];
                  for (int i = 0; i< itemList.count; i++) {
      
      
              /// separating VIDEO ID dictionary from items dictionary and string video id
                  id v_id0 = itemList[i];
                  NSDictionary * vid_id = v_id0[@"id"];
                  id v_id = vid_id;
                  NSString * video_ID = v_id[@"videoId"];
      
             //you can fill your local array for video ids at this point
      
                 //     [video_IDS addObject:video_ID];
      
              /// separating snippet dictionary from itemlist array
                  id snippet = itemList[i];
                  NSDictionary * snip = snippet[@"snippet"];
      
              /// separating TITLE and DESCRIPTION from snippet dictionary
                  id title = snip;
                  NSString * title_For_Video = title[@"title"];
                  NSString * desc_For_Video = title[@"description"];
      
          //you can fill your local array for titles & desc at this point 
      
                    //  [video_titles addObject:title_For_Video];
                     // [video_description addObject:desc_For_Video];
      
      
      
      
              /// separating thumbnail dictionary from snippet dictionary
      
                  id tnail = snip;
                  NSDictionary * thumbnail_ = tnail[@"thumbnails"];
      
              /// separating highresolution url dictionary from thumbnail dictionary
      
                  id highRes = thumbnail_;
                  NSDictionary * high_res = highRes[@"high"];
      
              /// separating HIGH RES THUMBNAIL IMG URL from high res dictionary
      
                  id url_for_tnail = high_res;
                  NSString * thumbnail_url = url_for_tnail[@"url"];
         //you can fill your local array for titles & desc at this point
      
                      [video_thumbnail_url addObject:thumbnail_url];
      
      
                  }
               // reload your tableview on main thread   
          //[self.tableView    performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
           performSelectorOnMainThread:@selector(reloadInputViews) withObject:nil waitUntilDone:NO];
      
      
            // you can log all local arrays for convenience
               //   NSLog(@"%@",video_IDS);
                //  NSLog(@"%@",video_titles);
                //  NSLog(@"%@",video_description);
                //  NSLog(@"%@",video_thumbnail_url);
              }
              else
              {
                  NSLog(@"an error occurred");
              }
          }
      }];
      
      }
      

      【讨论】:

        猜你喜欢
        • 2014-11-17
        • 1970-01-01
        • 2020-11-26
        • 2017-04-10
        • 2018-10-28
        • 1970-01-01
        • 2018-04-16
        • 2023-04-03
        • 2015-07-24
        相关资源
        最近更新 更多