Loading [MathJax]/extensions/tex2jax.js

2012年12月17日月曜日

c# youtube api 2 (Api呼んでみる)

やりたいこと
Developerkeyを入手して、Apiを呼び出してみるところまでやってみる。


準備
■参考にしたサイト
本家
https://developers.google.com/youtube/2.0/developers_guide_dotnet?hl=ja
デベロッパーキー入手の参考にしたサイト
http://ryushimiz.blogspot.jp/2010/02/youtube-api.html

手順1 Developerkeyの入手
上述のデベロッパーキー入手の参考にしたサイトを参考にしてdeveloperkeyを作成する。


手順2 Apiをコール
本家のページにあるサンプルをコピーして以下のようなコードを作成する。
YouTubeRequestSettingsをnewするところでは、developerkey飲みを引数とした。

※本家のサイトではclientIDなるものも引数としているが、
cleinetIDがなんなのかわからなかったことと、developerkeyの入手画面でももう必要ないみたいなことが書いてあったので無視した。
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. // youtube  
  8. using Google.GData.Client;  
  9. using Google.GData.Extensions;  
  10. using Google.GData.YouTube;  
  11. using Google.GData.Extensions.MediaRss;  
  12. using Google.YouTube;  
  13.   
  14. namespace YoutubeTest001  
  15. {  
  16.     class Program  
  17.     {  
  18.         static void Main(string[] args)  
  19.         {  
  20.             string developerKey = "作成したデベロッパーキー";  
  21.             YouTubeRequestSettings settings = new YouTubeRequestSettings("example app", developerKey);  
  22.             YouTubeRequest request = new YouTubeRequest(settings);  
  23.   
  24.             Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0");  
  25.             Video video = request.Retrieve<video>(videoEntryUrl);  
  26.             printVideoEntry(video);  
  27.   
  28.             Console.ReadKey();  
  29.         }  
  30.   
  31.         static void printVideoEntry(Video video)  
  32.         {  
  33.             Console.WriteLine("Title: " + video.Title);  
  34.             Console.WriteLine(video.Description);  
  35.             Console.WriteLine("Keywords: " + video.Keywords);  
  36.             Console.WriteLine("Uploaded by: " + video.Uploader);  
  37.             if (video.YouTubeEntry.Location != null)  
  38.             {  
  39.                 Console.WriteLine("Latitude: " + video.YouTubeEntry.Location.Latitude);  
  40.                 Console.WriteLine("Longitude: " + video.YouTubeEntry.Location.Longitude);  
  41.             }  
  42.             if (video.Media != null && video.Media.Rating != null)  
  43.             {  
  44.                 Console.WriteLine("Restricted in: " + video.Media.Rating.Country);  
  45.             }  
  46.   
  47.             if (video.IsDraft)  
  48.             {  
  49.                 Console.WriteLine("Video is not live.");  
  50.                 string stateName = video.Status.Name;  
  51.                 if (stateName == "processing")  
  52.                 {  
  53.                     Console.WriteLine("Video is still being processed.");  
  54.                 }  
  55.                 else if (stateName == "rejected")  
  56.                 {  
  57.                     Console.Write("Video has been rejected because: ");  
  58.                     Console.WriteLine(video.Status.Value);  
  59.                     Console.Write("For help visit: ");  
  60.                     Console.WriteLine(video.Status.Help);  
  61.                 }  
  62.                 else if (stateName == "failed")  
  63.                 {  
  64.                     Console.Write("Video failed uploading because:");  
  65.                     Console.WriteLine(video.Status.Value);  
  66.   
  67.                     Console.Write("For help visit: ");  
  68.                     Console.WriteLine(video.Status.Help);  
  69.                 }  
  70.   
  71.                 if (video.ReadOnly == false)  
  72.                 {  
  73.                     Console.WriteLine("Video is editable by the current user.");  
  74.                 }  
  75.   
  76.                 if (video.RatingAverage != -1)  
  77.                 {  
  78.                     Console.WriteLine("Average rating: " + video.RatingAverage);  
  79.                 }  
  80.                 if (video.ViewCount != -1)  
  81.                 {  
  82.                     Console.WriteLine("View count: " + video.ViewCount);  
  83.                 }  
  84.   
  85.                 Console.WriteLine("Thumbnails:");  
  86.                 foreach (MediaThumbnail thumbnail in video.Thumbnails)  
  87.                 {  
  88.                     Console.WriteLine("\tThumbnail URL: " + thumbnail.Url);  
  89.                     Console.WriteLine("\tThumbnail time index: " + thumbnail.Time);  
  90.                 }  
  91.   
  92.                 Console.WriteLine("Media:");  
  93.                 foreach (Google.GData.YouTube.MediaContent mediaContent in video.Contents)  
  94.                 {  
  95.                     Console.WriteLine("\tMedia Location: " + mediaContent.Url);  
  96.                     Console.WriteLine("\tMedia Type: " + mediaContent.Format);  
  97.                     Console.WriteLine("\tDuration: " + mediaContent.Duration);  
  98.                 }  
  99.             }  
  100.         }  
  101.     }  
  102. }  
  103.   
  104. </video>  


実行結果
こんなのが出力されたので、ひとまずApiの呼び出しには成功しているのではと思ってる。
------------------------------------
Title: Introduction to Google Data
Jeff Fisher walks through basic history and concepts behind the Google Data prot
ocol. Visit the documentation for more information and list of available APIs:
http://code.google.com/apis/gdata/
Keywords:
Uploaded by: googledevelopers
------------------------------------
0 件のコメント:
コメントを投稿