使用 android、phonegap、childBrowser 登录 facebook
Posted
技术标签:
【中文标题】使用 android、phonegap、childBrowser 登录 facebook【英文标题】:Login to facebook with android, phonegap, childBrowser 【发布时间】:2011-12-27 13:56:00 【问题描述】:我成功地使用 childBrowser 插件登录 Facebook:
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');
);
它返回“已登录”,但是当我尝试获取用户 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 的 2 种解决方案的混合,其中没有一种有效。 救命!?
【问题讨论】:
***.com/questions/16576977/… 【参考方案1】:我已使用以下代码获取 Facebook 用户的姓名,请尝试一次。
params='access_token='+accessToken;
$.get("https://graph.facebook.com/me",params,
function(response)
fbUesrName=response.name;
,"json");
【讨论】:
【参考方案2】:你需要有一个application on Facebook来生成访问令牌,然后你才能得到ID,但它会是加密的形式。
【讨论】:
以上是关于使用 android、phonegap、childBrowser 登录 facebook的主要内容,如果未能解决你的问题,请参考以下文章