1 / 55

Web API

Web API. Thành viên : Châu Hoàn Sơn – 0912392 Nguyễn Quốc Nhã – 0912317 Nguyễn Văn Lộc – 0912xxx. Nội dung tìm hiểu Khái niệm Lợi ích của web api Các dạng web api được cung cấp Cách sử dụng chung cho web api Giới thiệu youtube api Một số api mà youtube hỗ trợ Demo.

ozzy
Download Presentation

Web API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web API Thànhviên: ChâuHoànSơn – 0912392 NguyễnQuốcNhã – 0912317 NguyễnVănLộc – 0912xxx

  2. Nội dung tìmhiểu • Kháiniệm • Lợiíchcủa web api • Cácdạng web apiđượccungcấp • Cáchsửdụngchungcho web api • Giớithiệuyoutubeapi • Mộtsốapimàyoutubehỗtrợ • Demo

  3. Kháiniệm API: Application Programming Interface (Giaodiệnlậptrìnhứngdụng) • Là 1 dạngthểhiện, giúppháttriểnứngdụng 1 cáchdễdàng qua việchỗtrợrấtnhiềunhữnghoạtđộng / dịchvụgiúpchongườidùngcóthểtươngtácvớihệthống. • Việctươngtácđócóthểđượcđịnhnghĩa qua cáchàm, cácthamsố, cácbiếncóliênquanđếnhệthốngchính. VD: Window cócác API đểtươngtácvớicác service củahệthống, cácphầnmềmmặcđịnh…

  4. Kháiniệm Web API: là API dướidạng web • Làhệthốngthiếtlậpcácthôngđiệpdạng HTTP, NET/TCP,.. đểgửilên web, sauđónhậncácthôngđiệpphảnhồivề (thườnglàdạng JSON, XML cócấutrúcrõràng). • Cungcấpchongườidùng 1 dạngcácthểhiện, giúpchongườidùngcóthểtươngtácđượcvớidịchvụđượccungcấptrên web • Ngườidùngthông qua API đểsửdụngcácdịnhvụđó VD: cáchàm, cácgiaothức, gói tin gửiđi, các URL,..

  5. Lợiíchcủa web api • Giảiquyếtvấnđềvề dung lượnglưutrữ, tốcđộxửlý…. • Cậpnhậtrealtime (thờigianthực) • Tăngtínhtiệndụng • Giảmthờigian, chi phíkhipháttriểnứngdụng • …

  6. Cácdạng web apiđượccungcấp • Địachỉ web của API: Mỗi API sẽcóđịachỉ web tươngứng, truyvấncácđịachỉnàyvàcácthamsốtruyềnvàođểsửdụng. VD: Google map: http://maps.googleapis.com/maps/api/ Google Translator: http://translate.googleapis.com/translate_static Yahoo! Map Service: http://local.yahooapis.com/MapsService/

  7. Cácdạng web apiđượccungcấp • Cácthưviệnđộng: Web API đượcđónggóitrong 1 bộthưviện. Khisửdụng, gọicáchàmtrongđóđểsử dung, tươngtácvớidịchvụ ở trên Net. VD: Facebook SDK, Youtube Data API.. • CácthưviệnJavascript: Dùngchoviệcsửdụng Web API nhúngvàotrong website củangườidùng (códạnggầngiốngvớicáchsửdụngđịachỉ web API) VD: Google, Youtube, Youtube Player API,..

  8. Cáchsửdụngchungcho web api • Đốivới Web API dạng Link: • Dùngcác http request đểthựchiệnviệctruyvấnđến web service vànhậndữliệu. • Các http Request làcác link tươngứngtừngdịchvụmàhệthốngđóhỗtrợ; truyềnvàocácthamsố, sauđódùngnóđểtruyvấnlênhệthốngtrên Web, hệthốngsẽxửlývàtrảvềkếtquả.

  9. Cáchsửdụngchungcho web api • Đốivới Web API dạng Link:  VD: đoạn code sausẽtruyvấnlên Yahoo API, tìmvàlấy “Tên” củatỉnh ở tọađộKinhđộ, Vĩđộđượctruyềnvào. Kếtquảtrảvềlà 1 file Json, trongđócótêncủatỉnhcầnlấy. public WebClientwebClient; Uri uri = new Uri( "http://where.yahooapis.com/ geocode?&gflags=R&location=“+ [kinhđộ] + "+" + [vĩđộ]); webClient.DownloadStringAsync(uri);

  10. Cáchsửdụngchungcho web api • Đốivới Web API dạng SDK: • Download vàcàiđặt SDK • Add reference vàsửdụngnhữngphươngthứcmà web apicungcấp VD: Youtube API https://code.google.com/p/google-gdata/downloads/list

  11. Cáchsửdụngchungcho web api • Đốivới Web API dạng SDK: *Hiện video feed Feed<Video> videoFeed = request.Get<Video>(new Uri(feedUrl));printVideoFeed(videoFeed);static void printVideoFeed(Feed<Video> feed){foreach (Video entry in feed.Entries)  {printVideoEntry(entry);   }}

  12. Cáchsửdụngchungcho web api • Đốivới Web API dạngthưviện Java Script: • Sửdụngchínhcác link đến Web API đóđểlấythông tin, khởitạocácgiátrịchođốitượngvàthựchiện VD: link đếngoogle map apiđểlấy file javascript , hiểnthịgoogle map vàcácphím control trêntrìnhduyệt.

  13. Cáchsửdụngchungcho web api • Đốivới Web API dạngthưviện Java Script:

  14. Giớithiệuyoutubeapi www.youtube.com • Website chiasẻ video hàngđầuthếgiới • SốlượtsửdụnghàngngàycủaYoutubeđếnhơn 200.000 lượt (thốngkê 2010). • Ngườidùngcóthểđăngtảicác video, comment, lưu video vàodanhsáchưathích, tạo page riêng,.. vànhiềuứngdụngkhác.

  15. Giớithiệuyoutubeapi YoutubeApicungcấp 2 dạng: Data API và Player API • Data API: chophépngườidùngcóthểthựchiệnnhiềuthaotáctươngtựtrên website Youtube: Upload video, Sửađổidanhsáchưathích, tìmkiếm, comment, đánhgiá,.. • Player API: chophépngườidùngnhúngđoạn video vàotrongtrang web vàcóthểthựchiệncácthaotác Play, Pause, Stop, tănggiảmâmlượng,.. trựctiếptừ web củahọmàkhôngphảithông qua Youtube.

  16. Mộtsốapimàyoutubehỗtrợ • Video feed và entry • Truyxuấtvàtìmkiếm video • Upload video • Cậpnhậtvàxoá video • Sửdụngtínhnăngcộngđồng • Lưutrữvàchọnlựa video • Kíchhoạttươngtácngườidùng

  17. Mộtsốapimàyoutubehỗtrợ • Video feed và entry *Truyxuất video feed Feed<Video> videoFeed = request.Get<Video>(new Uri(feedUrl));printVideoFeed(videoFeed);static void printVideoFeed(Feed<Video> feed){foreach (Video entry in feed.Entries)  {printVideoEntry(entry); //xemmục 1.3 video entry contents  }}

  18. Mộtsốapimàyoutubehỗtrợ • Video feed và entry *Truyxuất video feed

  19. Mộtsốapimàyoutubehỗtrợ • Video feed và entry *Truyxuất video entry qua id Uri  videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/ api/videos/ADos_xW4_J0"); Video video = request.Retrieve<Video>(videoEntryUrl);printVideoEntry(video);

  20. Mộtsốapimàyoutubehỗtrợ • Video feed và entry *Truyxuất video entry qua id

  21. Mộtsốapimàyoutubehỗtrợ • Video feed và entry *Nội dung của video entry video.Title video.Description video.Keywords video.Uploader video.YouTubeEntry video.Media video.Status video.ReadOnly video.RatingAverage video.Contents …

  22. Mộtsốapimàyoutubehỗtrợ • Video feed và entry *Nội dung của video entry

  23. Mộtsốapimàyoutubehỗtrợ • Truyxuấtvàtìmkiếm video *Truyxuất feed theochuẩn

  24. Mộtsốapimàyoutubehỗtrợ • Truyxuấtvàtìmkiếm video *Truyxuất feed theochuẩn http://gdata.youtube.com/feeds/api/standardfeeds /FEED_IDENTIFIER http://gdata.youtube.com/feeds/api/standardfeeds /localeID/feedID_CATEGORY_NAME Vd: • http://gdata.youtube.com/feeds/api/standardfeeds • /JP/top_rated_Comedy

  25. Mộtsốapimàyoutubehỗtrợ • Truyxuấtvàtìmkiếm video *Truyxuất feed theochuẩn Feed<Video> videoFeed = request.GetStandardFeed(YouTubeQuery.MostPopular);printVideoFeed(videoFeed);

  26. Mộtsốapimàyoutubehỗtrợ • Truyxuấtvàtìmkiếm video *Truyxuất feed theochuẩn

  27. Mộtsốapimàyoutubehỗtrợ • Truyxuấtvàtìmkiếm video *Truyxuất video feed upload bởingườidùng http://gdata.youtube.com/feeds/api/users/username/uploads http://gdata.youtube.com/feeds/api/users/username/uploads/VIDEO_ID http://gdata.youtube.com/feeds/api/users/default/uploads (lấy feed dựavàoquyền user đãchứngthực) Uri uri = new Uri("http://gdata.youtube.com/feeds/api /users/GoogleDevelopers/uploads");Feed<Video> videoFeed = request.Get<Video>(uri); printVideoFeed(videoFeed);

  28. Mộtsốapimàyoutubehỗtrợ • Truyxuấtvàtìmkiếm video *Truyxuất video feed upload bởingườidùng

  29. Mộtsốapimàyoutubehỗtrợ • Truyxuấtvàtìmkiếm video *Lấynhững video liênquan Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api /videos/ADos_xW4_J0");Video video = request.Retrieve<Video>(videoEntryUrl);Feed<Video> relatedVideos = request.GetRelatedVideos(video);printVideoFeed(relatedVideos)

  30. Mộtsốapimàyoutubehỗtrợ • Truyxuấtvàtìmkiếm video *Lấynhững video liênquan

  31. Mộtsốapimàyoutubehỗtrợ • Truyxuấtvàtìmkiếm video *Tìmkiếm video YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);//order results by the number of views (most viewed first)query.OrderBy = "viewCount";// search for puppies and include restricted content in the search results// query.SafeSearch could also be set to YouTubeQuery.SafeSearchValues.Moderatequery.Query = "puppy";query.SafeSearch = YouTubeQuery.SafeSearchValues.None;Feed<Video> videoFeed = request.Get<Video>(query);printVideoFeed(videoFeed); (http://gdata.youtube.com/feeds/api/videos?q=keyword&safeSearch=none&orderby=viewCount)

  32. Mộtsốapimàyoutubehỗtrợ • Truyxuấtvàtìmkiếm video *Tìmkiếm video

  33. Mộtsốapimàyoutubehỗtrợ • Truyxuấtvàtìmkiếm video *Tìmkiếm video sửdụng category và keyword YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);AtomCategorycategory1 = new AtomCategory("News", YouTubeNameTable.CategorySchema);AtomCategorycategory2 = new AtomCategory("sports", YouTubeNameTable.KeywordSchema);AtomCategorycategory3 = new AtomCategory("football", YouTubeNameTable.KeywordSchema);query.Categories.Add(new QueryCategory(category1));query.Categories.Add(new QueryCategory(category2));query.Categories.Add(new QueryCategory(category3));Feed<Video> videoFeed = request.Get<Video>(query);printVideoFeed(videoFeed);

  34. Mộtsốapimàyoutubehỗtrợ • Truyxuấtvàtìmkiếm video *Tìmkiếm video sửdụng category và keyword

  35. Mộtsốapimàyoutubehỗtrợ • Upload video *Upload video trựctiếp Video newVideo = new Video();newVideo.Title ="My Test Movie";newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));newVideo.Keywords = "cars, funny";newVideo.Description = "My description";newVideo.YouTubeEntry.Private = false;newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag", YouTubeNameTable.DeveloperTagSchema));newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);// alternatively, you could just specify a descriptive string// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");newVideo.YouTubeEntry.MediaSource = new MediaFileSource("c:\\file.mov",  "video/quicktime");Video createdVideo = request.Upload(newVideo);

  36. Mộtsốapimàyoutubehỗtrợ • Upload video *Upload dựavàotrìnhduyệt Video newVideo = new Video();newVideo.Title ="My Test Movie";newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));newVideo.Keywords = "cars, funny";newVideo.Description = "My description";newVideo.YouTubeEntry.Private = false;newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag", YouTubeNameTable.DeveloperTagSchema));newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);// alternatively, you could just specify a descriptive string// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");FormUploadToken token = request.CreateFormUploadToken(newVideo);Console.WriteLine(token.Url);Console.WriteLine(token.Token);

  37. Mộtsốapimàyoutubehỗtrợ • Upload video *Kiểmtratrạngthái upload if (video.IsDraft){Console.WriteLine("Video is not live.");  string stateName = video.Status.Name;  if (stateName == "processing")  {Console.WriteLine("Video is still being processed.");  }else if (stateName == "rejected"){Console.Write("Video has been rejected because: ");Console.WriteLine(video.Status.Value);Console.Write("For help visit: ");Console.WriteLine(video.Status.Help);}else if (stateName == "failed"){Console.Write("Video failed uploading because:");Console.WriteLine(video.Status.Value);Console.Write("For help visit: ");Console.WriteLine(video.Status.Help);}

  38. Mộtsốapimàyoutubehỗtrợ • Cậpnhậtvàxoá video *Cậpnhậtthông tin video *Xoá video video.Description = "new description";Video updatedVideo = request.Update(video); request.Delete(video);

  39. Mộtsốapimàyoutubehỗtrợ • Sửdụngtínhnăngcộngđồng *Đánhgiá video *Truyxuất video comment video.Rating = 5;request.Insert(video.RatingsUri, video); Feed comments = request.GetComments(v); foreach (Comment c in comments.Entries) { Console.WriteLine(c.Content); }

  40. Mộtsốapimàyoutubehỗtrợ • Sửdụngtínhnăngcộngđồng *Thêm comment Comment c = new Comment();c.Content = "This is my comment.";request.AddComment(video, c); // retrieve the comments for the video Feed fc = request.GetComments(video); List comments = new List(fc.Entries); // create a reply to the third comment Comment c = comments[2]; Comment reply = new Comment(); reply.ReplyTo(c); reply.Content = "This is the reply text"; request.AddComment(video, reply);

  41. Mộtsốapimàyoutubehỗtrợ • Sửdụngtínhnăngcộngđồng *Truyxuất video responses *Thêm video response *Xoá video response Uri  videoEntryUrl =   new Uri("http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0");Video video = request.Retrieve<Video>(videoEntryUrl);Feed<Video> responses = request.GetResponseVideos(video);printVideoFeed(responses); request.Insert(video.ResponseUri, responseVideo); request.Insert(videoFeed, responseVideo); request.Delete(responseVideo);

  42. Mộtsốapimàyoutubehỗtrợ • Sửdụngtínhnăngcộngđồng *Đánhđấu video Complaint c = new Complaint();c.Type = ComplaintEntry.ComplaintType.PORN;c.Content = "This video offends my better sensibilities.";request.Insert(video.ComplaintUri, c);

  43. Mộtsốapimàyoutubehỗtrợ • Lưutrữvàthuthập video *Truyxuất video yêuthíchcủangườidùng *Thêm video yêuthích *Xoá video yêuthích Feed<Video> feed = request.GetFavoriteVideo(username);printVideoFeed(videoFeed); string videoEntryUrl = "http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0";YouTubeEntryvideoEntry = (YouTubeEntry) service.Get(videoEntryUrl);service.Insert(new Uri(feedUrl), videoEntry); request.Delete(favoriteVideo);

  44. Mộtsốapimàyoutubehỗtrợ • Lưutrữvàthuthập video *Truyxuất playlist củangườidùng *Truyxuấtthông tin playlist Feed<Playlist> userPlaylists = request.GetPlaylistsFeed(username);foreach (Playlist p in userPlaylists.Entries){Console.WriteLine("Title: " + p.Title);Console.WriteLine(p.Summary);Console.WriteLine("Number of entries: " + p.CountHint);} Feed<PlayListMember> list = request.GetPlaylist(p);foreach (Video v in list.Entries){printVideoEntry(v);}

  45. Mộtsốapimàyoutubehỗtrợ • Lưutrữvàthuthập video *Thêm playlist *Xoá playlist *Cậpnhật playlist Playlist pl = new Playlist();pl.Title = "this is the title";pl.Summary = "this is my new playlist";Playlist createdPlaylist = request.Insert(  new Uri("http://gdata.youtube.com/feeds/api/users/default/playlists"), pl); request.Delete(playlist); pl.Summary = "updated summary";request.Update(pl);

  46. Mộtsốapimàyoutubehỗtrợ • Lưutrữvàthuthập video *Thêm video vào playlist *Sửathông tin của playlist *Xoávideo trong playlist // For Playlist object pPlayListMember pm = new PlayListMember();// Insert <id> or <videoid> for video herepm.Id = VIDEOID;request.AddToPlaylist(p, pm); // move PlayListMember pm to the top of the playlistpm.Position = 1;request.Update(pm); request.Delete(playListMember);

  47. Mộtsốapimàyoutubehỗtrợ • Lưutrữvàthuthập video *Lấy subscription củangườidùng *Thêm subscription string feedUrl=  "http://gdata.youtube.com/feeds/api/users/GoogleDevelopers/subscriptions";YouTubeQuery query = new YouTubeQuery(feedUrl);SubscriptionFeedsubFeed = service.GetSubscriptions(query);foreach (SubscriptionEntry entry in subFeed.Entries){Console.WriteLine("Title: " + entry.Title.Text);Console.WriteLine(entry.Content.Src.Content);} Subscription s = new Subscription();s.Type = SubscriptionEntry.SubcriptionType.channel;s.UserName = "GoogleDevelopers";request.Insert(new Uri(YouTubeQuery.CreateSubscriptionUri(null)), s);

  48. Mộtsốapimàyoutubehỗtrợ • Lưutrữvàthuthập video *Theo dõi 1 danhsáchcác video yêuthích *Theo dõiplaylist Subscription s = new Subscription();s.Type = SubscriptionEntry.SubcriptionType.favorites;s.UserName= "GoogleDevelopers";request.Insert(new Uri(YouTubeQuery.CreateSubscriptionUri(null)), s); Subscription s = new Subscription();s.Type = SubscriptionEntry.SubcriptionType.playlist;string playlistId = "586D322B5E2764CF";s.PlaylistId = playlistId;request.Insert(new Uri(YouTubeQuery.CreateSubscriptionUri(null)), s);

  49. Mộtsốapimàyoutubehỗtrợ • Lưutrữvàthuthập video *Theo dõitheotừkhoátìmkiếm *Xoásubscription Subscription s = new Subscription();s.Type = SubscriptionEntry.SubcriptionType.query;s.QueryString = "puppies";request.Insert(new Uri(YouTubeQuery.CreateSubscriptionUri(null)), s); request.Delete(s);

  50. Mộtsốapimàyoutubehỗtrợ • Kíchhoạttươngtácngườidùng *Truyxuất profile củangườidùng string feedUrl = "http://gdata.youtube.com/feeds/api/users/GoogleDevelopers";ProfileEntry profile = (ProfileEntry) service.Get(feedUrl); ... profile.UserName profile.Age profile.Gender profile.Relationship profile.Books profile.Company profile.Description …

More Related