How to get YouTube channel name?

2020-07-16 12:37发布

I have searched in YouTube Documents and found nothing to get others channel name from a YouTube video.

That is,

I currently would like to get the channel name from a video, I only have the URL, how to get the channel name?

3条回答
Rolldiameter
2楼-- · 2020-07-16 12:44

You can do this easily by using YouTube Data API v3.

The latest part in the URL after "http://www.youtube.com/watch?v=" is your VIDEO_ID.

Just do a videos->list with setting part="snippet". Then you can grab snippet.channelId in the response.

Request would be:

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

for your example.

You can always try these using API explorer.

Great sample codes to get you started.

查看更多
看我几分像从前
3楼-- · 2020-07-16 12:58

For total newbies who are lost : consider a sample function that will help understand the entire cycle of fetch,parse,display etc and bring youtube channel's videos to your tableview specifically. im not writing the tableview part here

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

}
查看更多
▲ chillily
4楼-- · 2020-07-16 13:03

You can extract the video id from the URL, and then make an HTTP GET request:

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

where dQw4w9WgXcQ is the video id you're interested in. This returns a JSON response, with the channel name in the author field (click the link for an example).

For more information, see Retrieving Data for a Single Video and the rest of the YouTube API documentation.

See e.g. How do I find all YouTube video ids in a string using a regex? for some ways to get the video id from a YouTube URL.

查看更多
登录 后发表回答