one*_*ece 4 facebook facebook-iframe facebook-javascript-sdk
我想在呈现iframe登录以请求扩展权限之前检查已登录用户的权限,但是iframe弹出窗口被浏览器阻止(ff,chrome tests).我想避免这种情况,我很确定它因为js函数的结果是'嵌套的' - 我的js聪明才有限,所以请原谅.
我猜测如果我可以保留原始函数中的所有内容而不传递结果,浏览器仍然会将登录iframe视为"用户启动"而不是阻止.
但我无法做到这一点 - 例如,为什么以下结果导致数据=未定义.
var data = FB.api(
{
method: 'fql.query',
query: 'SELECT create_event FROM permissions WHERE uid=' + response.session.uid
});
alert(data); // = undefined
Run Code Online (Sandbox Code Playgroud)
我当前的完整代码是:
<button id="myButton" >Test Publish Event</button>
<script>
$('#myButton').bind('click', function() {
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
FB.api(
{
method: 'fql.query',
query: 'SELECT create_event FROM permissions WHERE uid=' + response.session.uid
},
function(response) {
if(response[0].create_event == 1) {
alert('permission granted');
} else {
alert('permission absent');
FB.login( function(response) {if (response.session) {
handleSessionResponse(response);
if (response.perms) {
// user is logged in and granted some permissions.
// perms is a comma separated list of granted permissions
alert(response.perms);
} else {
// user is logged in, but did not grant any permissions
}
} else {
// user is not logged in
}
}, {perms:'create_event,rsvp_event'});
}
}
);
</script>
Run Code Online (Sandbox Code Playgroud)
好极了 !同样的问题(没有相同的问题,但相同的所需功能)困扰了我很多,但最终在各种来源的研究HOURS后,我想出了一个解决方案!
1
var data = FB.api(
Run Code Online (Sandbox Code Playgroud)
这是错的!因为所有fb请求都是异步执行的,所以检索数据值的唯一方法是在执行后的请求中.前
FB.api(
{
method: 'fql.query',
query: 'SELECT create_event FROM permissions WHERE uid=' + response.session.uid
},function(response){
This function will run after the request is finished and ONLY inside here you can read the results data
ex alert(response.data);
});
}
});
Run Code Online (Sandbox Code Playgroud)
出于安全原因,Chrome会阻止弹出你的权限请求(firefox允许其他浏览器允许或不允许)
"只有从用户操作事件(例如.onclick)打开时才能允许弹出一个弹出窗口"因此您可以将该功能附加到允许的.onclick事件中.
3我检查用户是否具有使用您的应用所需权限的解决方案是:
FB.getLoginStatus(function(response) {
if (response.status.toString().indexOf("connected")>-1) {
initAll();
} else {
requestPermisions proceedure
}
Run Code Online (Sandbox Code Playgroud)
此函数检查用户是否已连接并以其他方式为您的应用授予了权限response.status ="unknown".
现在....
权限弹出块问题有2个解决方案.
第一个解决方案=将FB.login()函数附加到button的onclick事件.Ex.
ifUserNotgrandedPermisions{document.getElementByid("aLogInButton").onclick = function(){
FB.login(....blah blah blah);
};
Run Code Online (Sandbox Code Playgroud)
第二个解决方案和我实现的解决方案是将iFrame重定向到请求权限页面,而不是弹出
下面是一个完整的解决方案,它检查用户是否已登录并被授予权限...如果不是它要求用户登录然后请求权限(如果登录只询问权限)(如果有权限只是登录)
FB.init({appId: 'YourAPPID', status: true, cookie: true, xfbml: true, oauth : true});
FB.getLoginStatus(function(response) {
if (response.status.toString().indexOf("connected")>-1) {
initAll(); //User is connected and granted perms Good to go!
} else {
Run Code Online (Sandbox Code Playgroud)
//top.location更改浏览器网址而不是iframe的网址
//此网址专门用于为您的应用询问权限.您可以更改权限和appID
// redirect_uri =将其更改为您的应用程序画布页面
top.location=window.location="http://www.facebook.com/dialog/oauth/?scope=read_stream,publish_stream,friends_photos,friends_activities&client_id="yourAPPID(nobrackets)"&redirect_uri=http://apps.facebook.com/filtered_feed/&response_type=code";}});};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5501 次 |
| 最近记录: |