SlideShare ist ein Scribd-Unternehmen logo
1 von 36
BÀI BÁO CÁO

Cách làm việc với
Facebook
Nhóm 6:

Nguyễn Văn Uy 11520473
Trần Tiến Đại
11520039
Nguyễn Hữu Tuấn11520458

GVHD:

Phạm Thi Vương
Giới thiệu về facebook deverlopers
Giới thiệu về bộ thư viện facebook.dll
Tương tác với facebook qua facebook.dll
Xây dựng tính năng chat qua facebook server
Giới thiệu về facebook
deverlopers
Website: https://developers.facebook.com/
Giới thiệu về facebook
deverlopers
Website: http://facebooksdk.net/
Giới thiệu về facebook
deverlopers

Facebook SDK for .NET
Features
•
•
•
•
•
•

NuGet Packages Available (Facebook and Facebook channel)
Compatible with all Facebook Graph and REST API methods.
Supports photo/video uploads
Supports batch requests and etags
Supports Facebook’s most current authentication systems.
Sample applications and documentation are provided to get you started quickly.

Supported Platforms
•
•
•
•
•
•
•

.NET 3.5 (Client Profile and Full Profile)
.NET 4.0 (Client Profile and Full Profile)
.NET 4.5
Windows Store Apps
Silverlight 5
Windows Phone 7.1
Windows Phone 8
Giới thiệu về facebook deverlopers
Giới thiệu về bộ thư viện facebook.dll
Tương tác với facebook qua facebook.dll
Xây dựng tính năng chat qua facebook server
Giới thiệu về bộ thư viện
facebook.dll
“The Facebook SDK for .NET binaries are only distributed via nuget."
Vì vậy, để sử dụng bộ thư viên này chúng ta cài đặt trực tiếp thông qua NUGET trong
Visual Studio. Phiên bản hiện tại là: 6.4.2

“For those using older versions of Visual Studio that does not support NuGet Package
Manager, please download the command line version of NuGet.exe and run the
following command.“
nuget install Facebook
Giới thiệu về bộ thư viện
facebook.dll
Giới thiệu về facebook deverlopers
Giới thiệu về bộ thư viện facebook.dll
Tương tác với facebook qua facebook.dll
Xây dựng tính năng chat qua facebook server
Tương tác với facebook qua
facebook.dll
 Trước tiên, để có thể làm việc với facebook thông qua bộ thư viện đã tải
về, chúng ta phải tạo một app trên trang chủ dành cho nhà phát triển của
facebook
 Truy cập: https://developers.facebook.com/apps

Làm theo các hướng dẫn của facebook để đăng kí trở thành nhà phát triển.
Tương tác với facebook qua
facebook.dll

Sau khi đã đăng kí xong, tiến hành tạo một app theo hướng dẫn.
Giữ lại AppID và SecretID để sử dụng sau này:
Tương tác với facebook qua
facebook.dll

 Class FacebookClient:
Function

Description

object Get(...)

Makes a GET request to the Facebook server.

object Post(...)

Makes a POST request to the Facebook server.

Uri GetLoginUrl(object parameters);

Gets the Facebook OAuth login url.

Uri GetLogoutUrl(object parameters);

object ParseDialogCallbackUrl(Uri uri);

Gets the Facebook OAuth logout url.
Parses the dialog callback url to an object of the resulting data.

bool TryParseOAuthCallbackUrl(Uri url, out
Try parsing the url to Facebook.FacebookOAuthResult.
FacebookOAuthResult facebookOAuthResult);
Tương tác với facebook qua
facebook.dll
Function

Description

FacebookOAuthResult ParseOAuthCallbackUrl(Uri uri);

Parse the url to Facebook.FacebookOAuthResult.

object Delete(string path, object parameters);

Makes a DELETE request to the Facebook server.

void CancelAsync();

Cancels asynchronous requests.

void VerifyGetSubscription(string requestHubMode,
string requestVerifyToken, string requestHubChallenge);

Verify HTTP_X_HUB_SIGNATURE for HTTP
GET.

object VerifyPostSubscription(string
requestHttpXHubSignature, string requestBody, string
appSecret);

Verify HTTP_X_HUB_SIGNATURE for HTTP
POST.

.................................

.....................................
Tương tác với facebook qua
facebook.dll

 VD:

Lấy Url login của app

• Trước tiên, tạo một đối tượng FacebookClient:
FacebookClient _fb=new FacebookClient();
• Yêu cầu url login, sử dụng hàm:
Uri _loginUrl = _fb.GetLoginUrl(object parameter);

Vd: Uri _loginUrl=_fb.GetLoginUrl(new
parameter:
Client_id
App ID
{
redirect_uri client_id = _appID, web trả về khi yêu cầu thành
Địa chỉ trang
công.
redirect_uri = _redirectUri,
scope
Phạm vi truy xuất thông tin của ứng
scope=“user_about_me, publish_stream”,dụng.
display=“popup”, trình bày trang login.
display
Cách thức
Response_type=“token” về.
response_type
Kiểu đối tượng trả
});
Tương tác với facebook qua
facebook.dll

 VD:

Đăng nhập facebook bằng loginUrl đã có

• Tạo một đối tượng WebBrowser:
WebBrowser loginBrowser = new WebBrowser();
logiBrowser.Navigate(_loginUrl);
• Kiểm tra đăng nhập và nhận AccessToken:
void loginBrower_navigated(object sender, NavigationEventArgs e)
{
FacebookOAuthResult _result;
if(_fb.TryParseOAuthCallbackUri(e.uri, _result)
{
_accessToken=_result.AccessToken;
}
}
Tương tác với facebook qua
facebook.dll

 Hàm Get:

Function

Description

virtual object Get(string path);

Makes a GET request to the Facebook server.

virtual object Get(string path, object parameters);

Makes a GET request to the Facebook server.

virtual object Get(string path, object parameters,
Type resultType);

Makes a GET request to the Facebook server.

virtual void GetAsync(string path, object parameters,
object userState);

Makes an asynchronous GET request to the Facebook
server.

.................................

.................................
Tương tác với facebook qua
facebook.dll

 VD:

Lấy thông tin người dùng

• Để lấy thông tin người dùng ta sử dụng hàm Get:
_fb.Get(string path);
VD: var info=_fb.Get(“me”);
Tương tác với facebook qua
facebook.dll

 VD:

Lấy danh sách bạn bè

• Để lấy danh sách bạn bè ta sử dụng hàm Get:

_fb.Get(string path);

VD:

var info=_fb.Get(“me/friends”);
Tương tác với facebook qua
facebook.dll

 VD:

Lấy danh sách bạn bè online

• Để lấy danh sách bạn bè online ta sử dụng hàm Get:

_fb.Get(string path, object parameters);
VD:
VD:
•

Tương tác với facebook qua
facebook.dll
Lấy danh sách Albums của người dùng

Để lấy danh sách album ta dùng hàm:

_fb.Get(string path);
VD:

var info=_fb.Get(“me/albums”);
Tương tác với facebook qua
facebook.dll

 Hàm Post:

Function

Description

virtual object Post(object parameters);

Makes a POST request to the Facebook server.

virtual object Post(string path, object parameters);

Makes a POST request to the Facebook server.

virtual void PostAsync(object parameters);

Makes an asynchronous POST request to the
Facebook server.

virtual Task<object> PostTaskAsync(string path,
object parameters);

Makes an asynchronous POST request to the
Facebook server.

.................................

.................................
Tương tác với facebook qua
facebook.dll

 VD:

Post 1 status lên tường

• Để post status lên trang chủ ta sử dụng hàm Post:
_fb.Post(string Path, object parameter)
VD:
Tương tác với facebook qua
facebook.dll

 VD:

Post 1 hình lên tường

• Để post hình lên trang chủ ta sử dụng hàm Post:
_fb.Post(string Path, object parameter)
VD:
Giới thiệu về facebook deverlopers
Giới thiệu về bộ thư viện facebook.dll
Tương tác với facebook qua facebook.dll
Xây dựng tính năng chat qua facebook server
Xây dựng tính năng chat qua
facebook server

VẤN ĐỀ:

Bộ thư viên facebook.dll không hỗ trợ việc xây dựng tính năng chat.
Phía Facebook không hỗ trợ các hàm API để xây dựng tính năng
chat, mà chỉ hỗ trợ xây dựng tính năng chat thông qua giao thức
xmpp (Extensible Messaging and Presence Protocol).

GIẢI PHÁP:
Chúng ta sử dụng bộ thư viện agsxmpp.dll do công ty AG-Software cung cấp.
Đây là một bộ thư viện được cung cấp miễn phí và dựa trên phương thức
xmpp.
Link download bộ thư viện agsxmpp.dll:
http://www.ag-software.net/agsxmpp-sdk/
Xây dựng tính năng chat qua
facebook server

GIỚI THIỆU VỀ GIAO THỨC XMPP:

 Extensible Messaging and Presence Protocol (XMPP), trước đây là Jabber, là
giao thức mở và dựa trên nền tảng XML dùng trong nhắn tin nhanh (instant
messaging) và thông tin hiện diện trực tuyến (presence information). Theo Hội
Tiêu chuẩn XMPP (XMPP Standards Foundation, trước đây là Jabber Software
Foundation, JSF), phần mềm dựa trên Jabber được triển khai tại hàng ngàn máy
phục vụ trên Internet và được hơn 10 triệu người trên khắp thế giới sử dụng.
 Jeremie Miller khởi đầu dự án vào năm 1998; phiên bản đầu tiên được công bố
vào tháng năm 2000. Sản phẩm chính của dự án là jabberd, một trình phục vụ
(server) để từ đó các trình khách (client) kết nối đến và trao đổi tin nhắn. Trình
phục vụ này có thể tạo mạng Jabber riêng tư (như sau tường lửa) hoặc có thể tham
gia vào mạng Jabber công cộng toàn cầu. Đặc tính cốt lõi của Jabber là bản chất
của hệ thống tin nhắn nhanh phân tán và việc sử dụng streaming XML.
Xây dựng tính năng chat qua
facebook server

GIỚI THIỆU VỀ GIAO THỨC XMPP:

 Điểm đặc trưng của hệ thống Jabber là nó có các transport, còn được gọi là
gateway (cổng), cho phép người dùng truy cập mạng với các giao thức khác - như
AIM và ICQ (dùng OSCAR), MSN Messenger và Windows Messenger (dùng Dịch
vụ nhắn tin .NET - .NET Messenger Service), Yahoo! Messenger, SMS hay E-mail.
Không như các trình khách đa giao thức như Trillian hay Gaim, việc truy cập đến
các giao thức khác được Jabber cung cấp ở cấp độ trình phục vụ bằng cách truyền
thông tin qua các dịch vụ cổng đặc biệt chạy trên một máy tính ở xa. Bất cứ người
dùng nào cũng có thể 'đăng kí' với một trong các cổng này bằng cách cung cấp
thông tin cần thiết để đăng nhập vào mạng đó, và từ đó có thể liên lạc với người
dùng của mạng khác như thể họ là người dùng Jabber. Điều này có nghĩa là bất cứ
trình khách nào hỗ trợ đầy đủ giao thức Jabber đều có thể được dùng để truy cập bất
cứ mạng nào có cổng kết nối, mà không cần thêm dòng mã lệnh nào từ trình khách.
Xây dựng tính năng chat qua
facebook server

GIỚI THIỆU VỀ GIAO THỨC XMPP:

 Nền tảng của giao thức Jabber, hiện được Tổ chức Phần mềm Jabber quản lí,
đã được IETF chấp nhận làm giao thức standards-track dưới tên XMPP, với
RFC 3920. Nó thường được xem là đối thủ cạnh tranh với SIMPLE, dựa trên
giao thức SIP, để làm giao thức chuẩn cho nhắn tin nhanh và thông báo hiện
diện; tuy nhiên, thiết kế của XMPP được nhắm đến việc cung cấp các tiện ích
trình trung gian (middleware) liên ứng dụng và mục đích tổng quát.
Người dùng Jabber được xác định bằng tên người dùng và tên máy phục vụ,
cách nhau bằng dấu @. Căn cước này được gọi là Jabber ID hay JID.
Xây dựng tính năng chat qua
facebook server
JID:
 JID có dạng thức tên_người_dùng@tên_miền/tài_nguyên, tương tự như một địa
chỉ email.
 Người dùng Jabber có thể truy cập vào tài khoản của mình cùng lúc tại nhiều điểm
truy cập khác nhau, được xác định qua phần tài_nguyên, ví dụ
tên_người_dùng@tên_miền.com/cơ_quan và tên_người_dùng@tên_miền.com/nhà.
Không cần thiết chỉ định phần tài nguyên khi liên lạc với người dùng khác.
 Tương tự như Sendmail, người dùng Jabber có thể truy cập vào các giao thức
khác qua cổng giao tiếp Jabber (Jabber Transport), ví dụ JID của một địa chỉ là
tên_người_dùng%msn.com@msn.jabberserver.com.
 Tên người dùng trong JID dài tối đa 1023 ký tự và không được chứa @, :,
', ", <, >, &, khoảng trắng và ký tự điều khiển.
Xây dựng tính năng chat qua
facebook server

XÂY DỰNG CHỨC NĂNG CHAT:
Add thư viện agsXMPP vào project:

Khai báo để sử dụng:
Xây dựng tính năng chat qua
facebook server

XÂY DỰNG CHỨC NĂNG CHAT:

 Để có thể đăng nhập vào dịch vụ chat, ta cần phải có username của người dùng và
password. Username của người dùng có thể tìm thấy dưới dạng:
Username
https://www.facebook.com/[username].
chỗ này
 Sau khi đã có username và pass của người dùng, chúng ta tiến hành đăng nhập:
Khai báo:

XmppClientConnection xmpp;
xmpp = new XmppClientConnection(“chat.facebook.com”);
xmpp.Open(username, password);
Thread.Sleep(6000);

Dừng lại để chờ
server xác thực

if (xmpp.Authenticated == true)
{
Presence p = new Presence(ShowType.chat, "Online");
p.Type = PresenceType.available;
xmpp.Send(p);
};

Tên server chat
của facebook
Xây dựng tính năng chat qua
facebook server

XÂY DỰNG CHỨC NĂNG CHAT:
 Add bạn bè vào danh sách chat:

ID của bạn bè
xmpp.MessageGrabber.Add(new Jid("-" + friendId + "@chat.facebook.com"),
new BareJidComparer(),
new MessageCB(MessageCallBack),
null);

Hàm nhận sự kiện
khi có tin nhắn tới
Xây dựng tính năng chat qua
facebook server

XÂY DỰNG CHỨC NĂNG CHAT:
 Hàm nhận sự kiện tin nhắn đến:

void MessageCallBack(object sender, Message msg, object data)
{
if (msg.Body != null)
displayMessage();
}

Đây là hàm chịu trách
nhiệm xử lí và hiển thị tin
nhắn
Xây dựng tính năng chat qua
facebook server

XÂY DỰNG CHỨC NĂNG CHAT:
Gửi tin nhắn đi:

xmpp.Send(new Message(new Jid(“-” + friendID + "@chat.facebook.com"),
MessageType.chat,
messageBody));

ID của bạn bè sẽ
nhận tin nhắn
Nội dung tin
nhắn gửi đi
Xây dựng tính năng chat qua
facebook server
 Như vậy, chúng ta đã có thể hoàn thành được tính năng
chat facebook sử dụng thư viên agsxmpp.dll
Cảm ơn thầy và các
bạn đã quan tâm
theo dõi

Weitere ähnliche Inhalte

Empfohlen

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Empfohlen (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Slide báo cáo cuối kì lớp winp1.d21 facebook notification

  • 1. BÀI BÁO CÁO Cách làm việc với Facebook Nhóm 6: Nguyễn Văn Uy 11520473 Trần Tiến Đại 11520039 Nguyễn Hữu Tuấn11520458 GVHD: Phạm Thi Vương
  • 2. Giới thiệu về facebook deverlopers Giới thiệu về bộ thư viện facebook.dll Tương tác với facebook qua facebook.dll Xây dựng tính năng chat qua facebook server
  • 3. Giới thiệu về facebook deverlopers Website: https://developers.facebook.com/
  • 4. Giới thiệu về facebook deverlopers Website: http://facebooksdk.net/
  • 5. Giới thiệu về facebook deverlopers Facebook SDK for .NET Features • • • • • • NuGet Packages Available (Facebook and Facebook channel) Compatible with all Facebook Graph and REST API methods. Supports photo/video uploads Supports batch requests and etags Supports Facebook’s most current authentication systems. Sample applications and documentation are provided to get you started quickly. Supported Platforms • • • • • • • .NET 3.5 (Client Profile and Full Profile) .NET 4.0 (Client Profile and Full Profile) .NET 4.5 Windows Store Apps Silverlight 5 Windows Phone 7.1 Windows Phone 8
  • 6. Giới thiệu về facebook deverlopers Giới thiệu về bộ thư viện facebook.dll Tương tác với facebook qua facebook.dll Xây dựng tính năng chat qua facebook server
  • 7. Giới thiệu về bộ thư viện facebook.dll “The Facebook SDK for .NET binaries are only distributed via nuget." Vì vậy, để sử dụng bộ thư viên này chúng ta cài đặt trực tiếp thông qua NUGET trong Visual Studio. Phiên bản hiện tại là: 6.4.2 “For those using older versions of Visual Studio that does not support NuGet Package Manager, please download the command line version of NuGet.exe and run the following command.“ nuget install Facebook
  • 8. Giới thiệu về bộ thư viện facebook.dll
  • 9. Giới thiệu về facebook deverlopers Giới thiệu về bộ thư viện facebook.dll Tương tác với facebook qua facebook.dll Xây dựng tính năng chat qua facebook server
  • 10. Tương tác với facebook qua facebook.dll  Trước tiên, để có thể làm việc với facebook thông qua bộ thư viện đã tải về, chúng ta phải tạo một app trên trang chủ dành cho nhà phát triển của facebook  Truy cập: https://developers.facebook.com/apps Làm theo các hướng dẫn của facebook để đăng kí trở thành nhà phát triển.
  • 11. Tương tác với facebook qua facebook.dll Sau khi đã đăng kí xong, tiến hành tạo một app theo hướng dẫn. Giữ lại AppID và SecretID để sử dụng sau này:
  • 12. Tương tác với facebook qua facebook.dll  Class FacebookClient: Function Description object Get(...) Makes a GET request to the Facebook server. object Post(...) Makes a POST request to the Facebook server. Uri GetLoginUrl(object parameters); Gets the Facebook OAuth login url. Uri GetLogoutUrl(object parameters); object ParseDialogCallbackUrl(Uri uri); Gets the Facebook OAuth logout url. Parses the dialog callback url to an object of the resulting data. bool TryParseOAuthCallbackUrl(Uri url, out Try parsing the url to Facebook.FacebookOAuthResult. FacebookOAuthResult facebookOAuthResult);
  • 13. Tương tác với facebook qua facebook.dll Function Description FacebookOAuthResult ParseOAuthCallbackUrl(Uri uri); Parse the url to Facebook.FacebookOAuthResult. object Delete(string path, object parameters); Makes a DELETE request to the Facebook server. void CancelAsync(); Cancels asynchronous requests. void VerifyGetSubscription(string requestHubMode, string requestVerifyToken, string requestHubChallenge); Verify HTTP_X_HUB_SIGNATURE for HTTP GET. object VerifyPostSubscription(string requestHttpXHubSignature, string requestBody, string appSecret); Verify HTTP_X_HUB_SIGNATURE for HTTP POST. ................................. .....................................
  • 14. Tương tác với facebook qua facebook.dll  VD: Lấy Url login của app • Trước tiên, tạo một đối tượng FacebookClient: FacebookClient _fb=new FacebookClient(); • Yêu cầu url login, sử dụng hàm: Uri _loginUrl = _fb.GetLoginUrl(object parameter); Vd: Uri _loginUrl=_fb.GetLoginUrl(new parameter: Client_id App ID { redirect_uri client_id = _appID, web trả về khi yêu cầu thành Địa chỉ trang công. redirect_uri = _redirectUri, scope Phạm vi truy xuất thông tin của ứng scope=“user_about_me, publish_stream”,dụng. display=“popup”, trình bày trang login. display Cách thức Response_type=“token” về. response_type Kiểu đối tượng trả });
  • 15. Tương tác với facebook qua facebook.dll  VD: Đăng nhập facebook bằng loginUrl đã có • Tạo một đối tượng WebBrowser: WebBrowser loginBrowser = new WebBrowser(); logiBrowser.Navigate(_loginUrl); • Kiểm tra đăng nhập và nhận AccessToken: void loginBrower_navigated(object sender, NavigationEventArgs e) { FacebookOAuthResult _result; if(_fb.TryParseOAuthCallbackUri(e.uri, _result) { _accessToken=_result.AccessToken; } }
  • 16. Tương tác với facebook qua facebook.dll  Hàm Get: Function Description virtual object Get(string path); Makes a GET request to the Facebook server. virtual object Get(string path, object parameters); Makes a GET request to the Facebook server. virtual object Get(string path, object parameters, Type resultType); Makes a GET request to the Facebook server. virtual void GetAsync(string path, object parameters, object userState); Makes an asynchronous GET request to the Facebook server. ................................. .................................
  • 17. Tương tác với facebook qua facebook.dll  VD: Lấy thông tin người dùng • Để lấy thông tin người dùng ta sử dụng hàm Get: _fb.Get(string path); VD: var info=_fb.Get(“me”);
  • 18. Tương tác với facebook qua facebook.dll  VD: Lấy danh sách bạn bè • Để lấy danh sách bạn bè ta sử dụng hàm Get: _fb.Get(string path); VD: var info=_fb.Get(“me/friends”);
  • 19. Tương tác với facebook qua facebook.dll  VD: Lấy danh sách bạn bè online • Để lấy danh sách bạn bè online ta sử dụng hàm Get: _fb.Get(string path, object parameters); VD:
  • 20. VD: • Tương tác với facebook qua facebook.dll Lấy danh sách Albums của người dùng Để lấy danh sách album ta dùng hàm: _fb.Get(string path); VD: var info=_fb.Get(“me/albums”);
  • 21. Tương tác với facebook qua facebook.dll  Hàm Post: Function Description virtual object Post(object parameters); Makes a POST request to the Facebook server. virtual object Post(string path, object parameters); Makes a POST request to the Facebook server. virtual void PostAsync(object parameters); Makes an asynchronous POST request to the Facebook server. virtual Task<object> PostTaskAsync(string path, object parameters); Makes an asynchronous POST request to the Facebook server. ................................. .................................
  • 22. Tương tác với facebook qua facebook.dll  VD: Post 1 status lên tường • Để post status lên trang chủ ta sử dụng hàm Post: _fb.Post(string Path, object parameter) VD:
  • 23. Tương tác với facebook qua facebook.dll  VD: Post 1 hình lên tường • Để post hình lên trang chủ ta sử dụng hàm Post: _fb.Post(string Path, object parameter) VD:
  • 24. Giới thiệu về facebook deverlopers Giới thiệu về bộ thư viện facebook.dll Tương tác với facebook qua facebook.dll Xây dựng tính năng chat qua facebook server
  • 25. Xây dựng tính năng chat qua facebook server VẤN ĐỀ: Bộ thư viên facebook.dll không hỗ trợ việc xây dựng tính năng chat. Phía Facebook không hỗ trợ các hàm API để xây dựng tính năng chat, mà chỉ hỗ trợ xây dựng tính năng chat thông qua giao thức xmpp (Extensible Messaging and Presence Protocol). GIẢI PHÁP: Chúng ta sử dụng bộ thư viện agsxmpp.dll do công ty AG-Software cung cấp. Đây là một bộ thư viện được cung cấp miễn phí và dựa trên phương thức xmpp. Link download bộ thư viện agsxmpp.dll: http://www.ag-software.net/agsxmpp-sdk/
  • 26. Xây dựng tính năng chat qua facebook server GIỚI THIỆU VỀ GIAO THỨC XMPP:  Extensible Messaging and Presence Protocol (XMPP), trước đây là Jabber, là giao thức mở và dựa trên nền tảng XML dùng trong nhắn tin nhanh (instant messaging) và thông tin hiện diện trực tuyến (presence information). Theo Hội Tiêu chuẩn XMPP (XMPP Standards Foundation, trước đây là Jabber Software Foundation, JSF), phần mềm dựa trên Jabber được triển khai tại hàng ngàn máy phục vụ trên Internet và được hơn 10 triệu người trên khắp thế giới sử dụng.  Jeremie Miller khởi đầu dự án vào năm 1998; phiên bản đầu tiên được công bố vào tháng năm 2000. Sản phẩm chính của dự án là jabberd, một trình phục vụ (server) để từ đó các trình khách (client) kết nối đến và trao đổi tin nhắn. Trình phục vụ này có thể tạo mạng Jabber riêng tư (như sau tường lửa) hoặc có thể tham gia vào mạng Jabber công cộng toàn cầu. Đặc tính cốt lõi của Jabber là bản chất của hệ thống tin nhắn nhanh phân tán và việc sử dụng streaming XML.
  • 27. Xây dựng tính năng chat qua facebook server GIỚI THIỆU VỀ GIAO THỨC XMPP:  Điểm đặc trưng của hệ thống Jabber là nó có các transport, còn được gọi là gateway (cổng), cho phép người dùng truy cập mạng với các giao thức khác - như AIM và ICQ (dùng OSCAR), MSN Messenger và Windows Messenger (dùng Dịch vụ nhắn tin .NET - .NET Messenger Service), Yahoo! Messenger, SMS hay E-mail. Không như các trình khách đa giao thức như Trillian hay Gaim, việc truy cập đến các giao thức khác được Jabber cung cấp ở cấp độ trình phục vụ bằng cách truyền thông tin qua các dịch vụ cổng đặc biệt chạy trên một máy tính ở xa. Bất cứ người dùng nào cũng có thể 'đăng kí' với một trong các cổng này bằng cách cung cấp thông tin cần thiết để đăng nhập vào mạng đó, và từ đó có thể liên lạc với người dùng của mạng khác như thể họ là người dùng Jabber. Điều này có nghĩa là bất cứ trình khách nào hỗ trợ đầy đủ giao thức Jabber đều có thể được dùng để truy cập bất cứ mạng nào có cổng kết nối, mà không cần thêm dòng mã lệnh nào từ trình khách.
  • 28. Xây dựng tính năng chat qua facebook server GIỚI THIỆU VỀ GIAO THỨC XMPP:  Nền tảng của giao thức Jabber, hiện được Tổ chức Phần mềm Jabber quản lí, đã được IETF chấp nhận làm giao thức standards-track dưới tên XMPP, với RFC 3920. Nó thường được xem là đối thủ cạnh tranh với SIMPLE, dựa trên giao thức SIP, để làm giao thức chuẩn cho nhắn tin nhanh và thông báo hiện diện; tuy nhiên, thiết kế của XMPP được nhắm đến việc cung cấp các tiện ích trình trung gian (middleware) liên ứng dụng và mục đích tổng quát. Người dùng Jabber được xác định bằng tên người dùng và tên máy phục vụ, cách nhau bằng dấu @. Căn cước này được gọi là Jabber ID hay JID.
  • 29. Xây dựng tính năng chat qua facebook server JID:  JID có dạng thức tên_người_dùng@tên_miền/tài_nguyên, tương tự như một địa chỉ email.  Người dùng Jabber có thể truy cập vào tài khoản của mình cùng lúc tại nhiều điểm truy cập khác nhau, được xác định qua phần tài_nguyên, ví dụ tên_người_dùng@tên_miền.com/cơ_quan và tên_người_dùng@tên_miền.com/nhà. Không cần thiết chỉ định phần tài nguyên khi liên lạc với người dùng khác.  Tương tự như Sendmail, người dùng Jabber có thể truy cập vào các giao thức khác qua cổng giao tiếp Jabber (Jabber Transport), ví dụ JID của một địa chỉ là tên_người_dùng%msn.com@msn.jabberserver.com.  Tên người dùng trong JID dài tối đa 1023 ký tự và không được chứa @, :, ', ", <, >, &, khoảng trắng và ký tự điều khiển.
  • 30. Xây dựng tính năng chat qua facebook server XÂY DỰNG CHỨC NĂNG CHAT: Add thư viện agsXMPP vào project: Khai báo để sử dụng:
  • 31. Xây dựng tính năng chat qua facebook server XÂY DỰNG CHỨC NĂNG CHAT:  Để có thể đăng nhập vào dịch vụ chat, ta cần phải có username của người dùng và password. Username của người dùng có thể tìm thấy dưới dạng: Username https://www.facebook.com/[username]. chỗ này  Sau khi đã có username và pass của người dùng, chúng ta tiến hành đăng nhập: Khai báo: XmppClientConnection xmpp; xmpp = new XmppClientConnection(“chat.facebook.com”); xmpp.Open(username, password); Thread.Sleep(6000); Dừng lại để chờ server xác thực if (xmpp.Authenticated == true) { Presence p = new Presence(ShowType.chat, "Online"); p.Type = PresenceType.available; xmpp.Send(p); }; Tên server chat của facebook
  • 32. Xây dựng tính năng chat qua facebook server XÂY DỰNG CHỨC NĂNG CHAT:  Add bạn bè vào danh sách chat: ID của bạn bè xmpp.MessageGrabber.Add(new Jid("-" + friendId + "@chat.facebook.com"), new BareJidComparer(), new MessageCB(MessageCallBack), null); Hàm nhận sự kiện khi có tin nhắn tới
  • 33. Xây dựng tính năng chat qua facebook server XÂY DỰNG CHỨC NĂNG CHAT:  Hàm nhận sự kiện tin nhắn đến: void MessageCallBack(object sender, Message msg, object data) { if (msg.Body != null) displayMessage(); } Đây là hàm chịu trách nhiệm xử lí và hiển thị tin nhắn
  • 34. Xây dựng tính năng chat qua facebook server XÂY DỰNG CHỨC NĂNG CHAT: Gửi tin nhắn đi: xmpp.Send(new Message(new Jid(“-” + friendID + "@chat.facebook.com"), MessageType.chat, messageBody)); ID của bạn bè sẽ nhận tin nhắn Nội dung tin nhắn gửi đi
  • 35. Xây dựng tính năng chat qua facebook server  Như vậy, chúng ta đã có thể hoàn thành được tính năng chat facebook sử dụng thư viên agsxmpp.dll
  • 36. Cảm ơn thầy và các bạn đã quan tâm theo dõi