1
votes

How do I use YouTube JavaScript API v3 so that I can get ALL channels as well as all playlists for a given user name, as JSON, etc?

I used following references

https://stackoverflow.com/a/18767785/799593 does not seem to be working anymore.

==> Channels

Below is my code, calling the channel end-point. It basically performs HTTP GET operation using AngularJS, where "id" is the ChannelID, being passed by "query" parameter.

var getUrl = 'https://www.googleapis.com/youtube/v3/channels';
this.get = function (apiKey, query) {
    var parameters = {
        kind: 'youtube#channel',
        part: 'contentDetails',
        id: query,
        maxResults: 3,
        key: apiKey
    }
    return $http({
        url: getUrl,
        method: 'GET',
        params: parameters
    })
};

This query basically can only return the IDs for Uploaded Videos and Liked Videos for a given YouTube channel. Instead, I am looking to get a list of all Channels, for a given user.

I also tried following:

this.getByUserName = function (apiKey, query) {
var parameters = {
    kind: 'youtube#channel',
    part: 'contentDetails',
    forUsername: query,
    maxResults: 5,
    key: apiKey
}
return $http({
    url: getUrl,
    method: 'GET',
    params: parameters
})

};

But it only seem to return the "main channel", in other words, if I query with forUsername="Google", I get UCK8sQmJBp8GCxrOtXWBpyEA ... so in contradiction to my interpretation in the "mind-map" below, the correct hierarchy might be actually linking only one channel for each user account and such channel can have multiple playlists. But this actually does look confusing and over-thinked approach from Google/YouTube.

e.g. if user passes "Google" then the API would reply with all channels we see on https://www.youtube.com/user/Google/channels

==> Playlists

This looked relatively straight forward but the API basically seem to be limited to a comma separated list of YouTube playlist IDs, but the question is how to get them in first place, given a UserID?

I am interested to get results equivalent to: https://www.youtube.com/user/UserID/playlists but following is what I have as of now.

var getUrl = 'https://www.googleapis.com/youtube/v3/playlists';
this.get = function (apiKey, query) {
    var parameters = {
        kind: 'youtube#playlist',
        part: 'contentDetails',
        id: query,
        maxResults: 10,
        pageToken: '',
        key: apiKey
    }
    return $http({
        url: getUrl,
        method: 'GET',
        params: parameters
    })
};

I'd conclude my question with this "mind-map" of YouTube's hierarchy and with annotated example of Google with links. I am not sure if this is the correct representation, but I believe if I know the correct hierarchy it might give me a hint as to how to I get YouTube Channels and PlayLists for a given user using YouTube JavaScript API v3.

http://i.imgur.com/GWoZvCE.png (Full size image at: http://i.imgur.com/GWoZvCE.png)

1

1 Answers

0
votes

I've written a code in ReacJS for playlists only. Hope this will give you some idea. I'm using google APIs. Its working for me. Just replace my channel id and api key with yours.

import React, { Component } from "react";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: [],
      isLoaded: false
    };
  }

  componentDidMount() {
    fetch(
      "https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=<YOUR-CHANNEL-ID>&key=<YOUR-API-KEY>"
    )
      .then(res => res.json())
      .then(json => {
        this.setState({
          isLoaded: true,
          items: json.items
        });
      });
  }

  render() {
    var { isLoaded, items } = this.state;
    var i=0;
    if (!isLoaded) return <div>Loading...</div>;
    return (
      <div>
        <ul>
        {
          items.map(item => (
            <li key={item.id}>
              Playlist: {items[i++].snippet.title}
            </li>
          ))
        }
        </ul>
      </div>
    );
  }
}

export default App;

Here is the output sample output