Transcript Flickr API

Flickr API
0712084
0712167
Giới thiệu
 Flickr® là ứng dụng quản lý và
chia sẻ ảnh trực tuyến hàng đầu
hiện nay.
 Mỗi phút Flickr nhận hơn 3000
hình; đến 19 tháng 9/2010, Flickr
nhận hình thứ 5 tỷ
Flickr API
Thương mại/ phi thương mại
 Free!!!
 Có thể tính phí với một số
trường hợp.
Flickr API
Flickr API
Tài liệu, công cụ
 http://www.flickr.com/services/api/
 http://www.flickr.com/services/developer/
 http://code.flickr.com/
 API kit: có hơn 20 API kit cho
15 ngôn ngữ lập trình
Flickr API
Tính năng
 - Upload (đồng bộ và không
đồng bộ)
 - Đưa ảnh lên blog
 - Lấy danh sách liên lạc
 - Truy xuất danh sách ưa
thích
 - Truy xuất phòng tranh
 - Tìm và lấy thông tin nhóm
 - Lấy danh sách thành viên
nhóm
 - Truy xuất ảnh trong nhóm
 - Lấy danh sách ảnh hấp dẫn
 …
 - Tìm người, lấy thông tin,
lấy ảnh
 - Truy xuất ảnh và thông tin
liên quan
 - Biến đổi ảnh (hiện chỉ mới
có quay ảnh)
 - Truy xuất bộ ảnh (photoset)
và bình luận
 - Lấy thông tin Flickr API
(danh sách hàm, chi tiết
hàm)
 - Lấy các thông tin thống kê
 - Lấy tag
 …
Flickr API
Gọi hàm (REST)
 http://api.flickr.com/services/rest/?
method=<funcname>&<argname>=<value>
 Vd: http://…/?method=flickr.test.echo&name=value
 Ngoài ra có thể gửi theo định dạng
XML-RPC, SOAP.
Flickr API
Định dạng trả về
 <?xml version="1.0" encoding="utf-8" ?>
 <rsp stat="ok">

[xml-payload-here]
 </rsp>
 <?xml version="1.0" encoding="utf-8" ?>
 <rsp stat="fail">

<err code="[error-code]" msg="[error-message]" />
 </rsp>
 Ngoài ra có thể chỉ định nhận kết quả trả về định dạng XMLRPC, SOAP, JSON, PHP
Flickr API
Phát triển ứng dụng
 Xin API key
Key thương mại
 Cần mô tả rõ mục đích để được
xét duyệt
 Kinh doanh độc lập, quy mô
nhỏ, hộ gia đình thì không xem
như ứng dụng thương mại
Flickr API
Xin API key
 http://www.flickr.com/services/apps/create/apply/
Flickr API
Cần giao tiếp với tài
khoản người dùng?
 Phải xin phép người
dùng
Chứng thực (authentication)
1. Lấy frob (~session id)

flickr.auth.getFrob
2. Người dùng đăng nhập và
chứng thực ứng dụng
3. Lấy thẻ chứng thực

flickr.auth.getToken
Flickr API
Ví dụ: lấy frob
String method = "flickr.auth.getFrob";
String apiSign = createMD5(_secret + "api_key" + _apiKey +
"method" + method);
String getFrobURL
getFrobURL +=
getFrobURL +=
getFrobURL +=
= "http://flickr.com/services/rest/?";
"method=" + method;
"&api_key=" + _apiKey;
"&api_sig=" + apiSign;
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(getFrobURL);
HttpWebResponse respond =
(HttpWebResponse)request.GetResponse();
StreamReader str = new
StreamReader(respond.GetResponseStream());
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(str.ReadToEnd());
Flickr API
_frob=xdoc.SelectSingleNode(".//frob").InnerText;
 Dùng API Kit !!
Flickr.NET
 http://flickrnet.codeplex.com/
 Thư viện Flickr.NET được viết hoàn
toàn bởi C# và có thể được truy xuất
từ:






Net Framework 2.0 trở lên.
.Net Compact Framework 2.0 SP1 trở lên.
Silverlight 3.0 trở lên.
Windows 7 Phone
Mono
Monotouch for iPhone
Flickr API
Ví dụ Flickr.NET
 Lấy frob
Flickr flickr = new Flickr(ApiKey, SharedSecret);
tempFrob = flickr.AuthGetFrob();
 Chứng thực
string flickrUrl = flckr.AuthCalcUrl(tempFrob, AuthLevel.Write);
System.Diagnostics.Process.Start(flickrUrl);
 Lấy thẻ chứng thực
Auth auth = flickr.AuthGetToken(tempFrob);
flickr.ApiToken = auth.Token;
Flickr API
Ví dụ Flickr.NET
 Upload ảnh
string
string
string
string
string
file = "test.jpg";
title = "Test Photo";
descripton = "This is the description of the photo";
tags = "tag1,tag2,tag3";
photoId = flickr.UploadPicture(file, title, dscription, tags);
 Cập nhật ảnh
flickr.PhotosSetMeta(photoId, "New Title", "New Description");
Flickr API
Ví dụ Flickr.NET
 Đưa ảnh vào photoset
Photosets sets = flickr.PhotosetsGetList();
Photoset set = sets.PhotosetCollection[0];
flickr.PhotosetsAddPhoto(set.PhotosetId, photoId);
Flickr API