|
我們將繼續Facebook平台更安全,使用戶從舊的Facebook的身份驗證系統和HTTP的應用程序轉換到OAuth的2.0和HTTPS。作為這些努力的一部分,我們已經發布了更新版本的JavaScript SDK提供支持OAuth的2.0。默認情況下,支持OAuth的2.0是選入,使所有平台使用JavaScript SDK的應用程序,將不間斷地繼續工作,而你過渡到使用新的OAuth的2.0功能。
要啟用OAuth的2.0功能,你只需要包括一個OAuth的的參數FB.init,並將其設置為true:- FB.init({
- appId : YOUR_APP_ID,
- // other parameters,
- oauth : true
- });
複製代碼 OAuth的2.0 OAuth的參數 如果省略或設置為false,將繼續禁用的JavaScript SDK將工作,因為它沒有這些變化之前。
當配置使用OAuth 2.0,會話被替換的授權響應。這次會議是一個Facebook的具體結構,封閉的訪問令牌和一個會話密鑰,其中任何一個可以使用API調用。授權響應封裝OAuth2兼容的數據和一個訪問令牌,用戶ID,簽名的要求,和到期時間。
支持登錄,註銷,登錄狀態大致相同。您的回調函數現在應該指望一個參數,將不包括會話屬性,如果OAuth的參數設置為true 將包括authResponse的屬性。改變之前,你可能已經調用如下FB.login:- FB.login(function(response) {
- if (response.session) {
- console.log("User is connected to the application.”);
- var accessToken = response.session.access_token;
- }
- });
複製代碼 使用OAuth 2.0啟用,上面的調用將被替換:- FB.login(function(response) {
- if (response.authResponse) {
- console.log("User is connected to the application.”);
- var accessToken = response.authResponse.accessToken;
- }
- });
複製代碼 至於會議,授權記錄可安裝時FB.init包括稱為authResponse參數和附加到一個登錄的用戶的有效的授權響應。而許多開發商將存儲在數據庫中的會話,你現在可能,而不是存儲授權響應。
一個簡單的,但更詳細的例子,說明一個典型的認證流和API調用級聯所示:- FB.login(function(response) {
- if (response.authResponse) {
- console.log('Welcome! Fetching your information.... ');
- FB.api('/me', function(response) {
- console.log('Good to see you, ' + response.name + '.');
- FB.logout(function(response) {
- console.log('Logged out.');
- });
- });
- } else {
- console.log('User cancelled login or did not fully authorize.');
- }
- }, {scope: 'email'});
複製代碼 FB.getLoginStatus像以前一樣,除了你的回調收到授權響應連接的用戶(而不是一個會話)。- FB.getLoginStatus(function(response) {
- if (response.status === 'connected') {
- // the user is logged in and connected to your
- // app, and response.authResponse supplies
- // the user’s ID, a valid access token, a signed
- // request, and the time the access token
- // and signed request each expire
- var uid = response.authResponse.userID;
- var accessToken = response.authResponse.accessToken;
- } else if (response.status === 'not_authorized') {
- // the user is logged in to Facebook,
- //but not connected to the app
- } else {
- // the user isn't even logged in to Facebook.
- }
- });
複製代碼 您仍然可以訂閱像以前那樣的事件集合:auth.login,auth.logout,auth.statusChange仍然工作,但auth.sessionChange已被廢棄,被替換auth.authResponseChange。當然,auth.sessionChange將繼續工作,直到至少在10月1日的過渡時間,並已啟用時OAuth2 auth.authResponseChange是唯一有關。OAuth的2.0啟用時,授權的反應,而不是會議傳遞給所有連接的用戶回調。
全部工作下面的例子:- <!DOCTYPE html>
- <html xmlns:fb="https://www.facebook.com/2008/fbml">
- <head>
- <title>
- New JavaScript SDK
- </title>
- </head>
- <body>
-
- <div id="fb-root"></div>
- <h2>Updated JS SDK example</h2><br />
- <div id="user-info"></div>
- <p><button id="fb-auth">Login</button></p>
- <script>
- window.fbAsyncInit = function() {
- FB.init({ appId: 'YOUR_APP_ID',
- status: true,
- cookie: true,
- xfbml: true,
- oauth: true});
- function updateButton(response) {
- var button = document.getElementById('fb-auth');
-
- if (response.authResponse) {
- //user is already logged in and connected
- var userInfo = document.getElementById('user-info');
- FB.api('/me', function(response) {
- userInfo.innerHTML = '<img src="https://graph.facebook.com/'
- + response.id + '/picture">' + response.name;
- button.innerHTML = 'Logout';
- });
- button.onclick = function() {
- FB.logout(function(response) {
- var userInfo = document.getElementById('user-info');
- userInfo.innerHTML="";
- });
- };
- } else {
- //user is not connected to your app or logged out
- button.innerHTML = 'Login';
- button.onclick = function() {
- FB.login(function(response) {
- if (response.authResponse) {
- FB.api('/me', function(response) {
- var userInfo = document.getElementById('user-info');
- userInfo.innerHTML =
- '<img src="https://graph.facebook.com/'
- + response.id + '/picture" style="margin-right:5px"/>'
- + response.name;
- });
- } else {
- //user cancelled login or did not grant authorization
- }
- }, {scope:'email'});
- }
- }
- }
- // run once with current status and whenever the status changes
- FB.getLoginStatus(updateButton);
- FB.Event.subscribe('auth.statusChange', updateButton);
- };
-
- (function() {
- var e = document.createElement('script'); e.async = true;
- e.src = document.location.protocol
- + '//connect.facebook.net/en_US/all.js';
- document.getElementById('fb-root').appendChild(e);
- }());
- </script>
- </body>
- </html>
複製代碼 更新OAuth的2.0路線圖
除了 提供新的JavaScript SDK,我們也更新我們的開發應用,這種轉變,以幫助你:
改名為“畫布為2.0 OAuth的”遷移到“為畫布signed_request。”這有助於澄清啟用此設置時,你的畫布應用將收到signed_request的參數。
創建一個新的遷移設置為“OAuth的遷移”,以幫助驗證您已達到OAuth的遷移時間表。啟用此遷移設置驗證您正在使用的訪問令牌,而不是使用傳統的認證。 |
|