A Xamarin Library for handling YouTube API requests
The Plugin could be created with only the api key or both an api key and an access token. provididng an acces token allows for more functionality
using YoutubeLibrary.youtube;
public MainWindow()
{
string ApiKey = "API_KEY";
string Accesstoken = "ACCESS_TOKEN";
YoutubeClient youtube = new YoutubeClient(ApiKey);
YoutubeClient youtube2 = new YoutubeClient(ApiKey, Accesstoken);
}
An example of Api requests and the corresponding parts of the code
POST https://youtube.googleapis.com/youtube/v3/playlists?part=snippet&part=contentDetails&prettyPrint=true&key=[API_KEY] HTTP/1.1
A single parameter is:
part=snippet
Combination of multiple parameters
part=contentDetails&prettyPrint=true
Parameter[] parameter = {new Parameter("prettyPrint", "true"), new Parameter("alt", "json")};
Bodies are json objects, the Body_Item class is used to acheive this Json structure
Body_Item class can store a single value
var b= new Body_Item("itemcount", "1")
//The equilavent
{
"itemCount": 1
}
Or multiple Body_Item classes
Body_Item contentDetails = new Body_Item("contentDetails");
contentDetails.values.Add(new Body_Item("itemcount", "1"));
contentDetails.values.Add(new Body_Item("itemcount", "2"));
"contentDetails": {
"itemCount": 1,
"itemCount": 2
}
*/
With this you are able to form Json objects
Example: The code equivalent of this Json
{
"contentDetails": {
"itemCount": 1
},
"snippet": {
"title": "playlistsTitle",
"description": "playlistsDescription",
"localized": {
"title": "local",
"description": "localDescription"
}
}
}
Body_Item snippetItems = new Body_Item("snippet");
snippetItems.values.Add(new Body_Item("title", "playlistTitle"));
snippetItems.values.Add(new Body_Item("description", "playlistsDescription"));
snippetItems.values.Add(new Body_Item("localized", new List<Body_Item> { new Body_Item("title", "local"), new Body_Item("description", "LocalDescription") }));
Body_Item contentDetails = new Body_Item("contentDetails");
contentDetails.values.Add(new Body_Item("itemcount", "1"));
var body = new List<Body_Item> { contentDetails, snippetItems}
To get the playlists calls the youtubeClient.playlistsList.getPlaylistAsync(String[] part, bool Mine) methods. This is an overloaded methood that takes in the following : the parts, Mine, parameter(optional);
public Main()
{
YoutubeClient youtubeClient = new YoutubeClient(ApiKey, token);
PlaylistResponse Playlists = await youtubeClient.playlistsList.getPlaylistAsync(new string[] { "snippet" }, true);
}
And then you could do
Parameter[] parameter = {new Parameter("prettyPrint", "true"), new Parameter("alt", "json")};
PlaylistResponse Playlists = await youtubeClient.playlistsList.getPlaylistAsync(new string[] { "snippet" }, true, parameter);
string id="playlist_id";
var response = await youtubeClient.playlistsList.deletePlaylistAsync(id);
To insert a playlist you call the overloaded insertPlaylistAsync Methods
PlaylistResponse playlist = await youtubeClient.playlistsList.insertPlaylistAsync("playlistTitle", "Description", new string[] { "snippet" });
You could optionally add parameters
PlaylistResponse playlist = await youtubeClient.playlistsList.insertPlaylistAsync("playlistTitle", "Description", new string[] { "snippet", }, parameter );
string channelName = "Google Developers";
ChannelResponse channelResponse = await youtubeClient.channel.searchByUsernameAsync(channelName);
string id = "Channel_Id";
ChannelResponse channelResponse = await youtubeClient.channel.searchByIdAsync(id);
ChannelResponse channelResponse = await youtubeClient.channel.getMyChannelAsync(true);
This method here can be used to make any sort of requests
Task<HttpResponseMessage> makeRequestAsync(Method method, string youtube, string resource, string stringParameter, Parameter[] parameter = null, List<Body_Item> body = null)
Here you specify the HTTPMethod, the Http request(youtube), the resource, parameters and optionally, the body. The method returns the result as a HttpResponseMessage
HttpResponseMessage result = await youtubeClient.anyRequest.makeRequestAsync(Method.GET, "https://youtube.googleapis.com/youtube/v3/", "playlists?", "part=snippet&part=contentDetails&mine=true");
OR
HttpResponseMessage result = await youtubeClient.anyRequest.makeRequestAsync(Method.GET, "https://youtube.googleapis.com/youtube/v3/", "playlists?", "", new Parameter[]{new Parameter("part","contentDetatils")});