2011-12-27 52 views
0

我設法登錄到Facebook上與childBrowser插件沒有任何問題:登錄與Android,的PhoneGap至Facebook,childBrowser

function deviceStart() { 

    FB.init({ appId: "xxxxxxxxxxxxxx", nativeInterface: PG.FB }); 

}; 

function onPubFacebookBtn(){ // I call this from a button 
var my_client_id = "xxxxxxxxxxxxx", 
my_redirect_uri = "http://www.facebook.com/connect/login_success.html", 
my_type   = "user_agent", 
my_display  = "touch" 

var authorize_url = "https://graph.facebook.com/oauth/authorize?"; 
authorize_url += "client_id="+my_client_id; 
authorize_url += "&redirect_uri="+my_redirect_uri; 
authorize_url += "&display="+my_display; 
authorize_url += "&scope=publish_stream,user_photos,email,user_online_presence,offline_access" 

window.plugins.childBrowser.onLocationChange = facebookLocChanged; 
window.plugins.childBrowser.onClose = closed; 
window.plugins.childBrowser.showWebPage(authorize_url); 

} 

function facebookLocChanged(loc){ 

if (/login_success/.test(loc)) { 
    var fbCode = loc.match(/code=(.*)$/)[1] 

    localStorage.setItem('pg_fb_session', JSON.stringify(fbCode)); 

    FB.Auth.setSession(fbCode, 'connected'); 
    window.plugins.childBrowser.close(); 
}} 

當我測試,如果應用程序與

function getLoginStatus() { 
FB.getLoginStatus(function(response) { 
if (response.session) { 
    alert('logged in'); 
} else { 
    alert('not logged in'); 
} 
}); 
} 

登錄它返回「loged in」,但是當我嘗試獲取用戶ID時,出現錯誤,說我需要激活的訪問令牌:

function me() { 
FB.api('/me', function(response) { 
    if (response.error) { 
     alert(JSON.stringify(response.error)); 
    } else { 
     var data = document.getElementById('data'); 
      response.data.forEach(function(item) { 
      var d = document.createElement('div'); 
      d.innerHTML = item.name; 
      data.appendChild(d); 
     }); 
     } 
}); 
} 

這是兩種解決方案的混合體,用於登錄到其中沒有任何作品的Facebook。 幫助!?

+0

http://stackoverflow.com/questions/16576977/is-there-any-facebook-plugin-for-phonegap-2-7-0/16579592#16579592 – 2013-06-11 03:52:39

回答

2

我已經使用下面的代碼來獲取Facebook用戶的名字,請嘗試一次。

params='access_token='+accessToken; 
      $.get("https://graph.facebook.com/me",params, 
      function(response){ 
       fbUesrName=response.name; 
       },"json");