使用新的 facebook javascript sdk 检查扩展权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3388367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:23:56  来源:igfitidea点击:

check for extended permissions with new facebook javascript sdk

javascriptfacebook

提问by choise

is there a way to check in my application (canvas) if the user has extended permission (for example "stream.publish")?

如果用户具有扩展权限(例如“stream.publish”),有没有办法检查我的应用程序(画布)?

i only can find a solution for the old sdk.

我只能找到旧sdk的解决方案。

thanks!

谢谢!

回答by Tim

Update at the end of 2011:

2011年底更新:

FB.api('/me/permissions', function (response) {
    console.log(response);
} );

console output:

控制台输出:

{
    data: [
        {
            create_note: 1,
            installed: 1,
            photo_upload: 1,
            publish_stream: 1,
            share_item: 1,
            status_update: 1,
            video_upload: 1,
        }
    ]
}

回答by choise

this solution is deprecated at the end of 2011, please use the answer that is marked as accepted above

此解决方案已于 2011 年底弃用,请使用上面标记为已接受的答案

found a solution myself

自己找到了解决办法

function check_ext_perm(session,callback) {
    var query = FB.Data.query('select publish_stream,read_stream from permissions where uid={0}', session["uid"]);
    query.wait(function(rows) {
        if(rows[0].publish_stream == 1 && rows[0].read_stream == 1) {
            callback(true);
        } else {
            callback(false);
        }
    });
};

this will check for publish_streamand read_stream

这将检查publish_streamread_stream

example use:

示例使用:

check_ext_perm(response.session, function(isworking) {
   if(isworking) {
      // post something to wall
   } else {
      // ask for login
   }
});

回答by jmlv21104

FB.ui({
    method: 'permissions.request',
    perms: 'user_website',
    display: 'popup'
    },function(response) {
        alert(response.toSource());
        if (response && response.perms) {
            alert('Permissions granted: '+response.perms);
        } else if (!response.perms){
            alert('User did not authorize permission(s).');
        }
});

回答by tantalor

Generalized choise's function,

广义选择函数,

function check_permissions (permissions, uid, cb) {
  FB.Data
    .query('select {0} from permissions where uid={1}', permissions, uid)
    .wait(function (res) {
      if (!res || res.length < 1) return cb(false);
      var row = res[0];
      var aperm = permissions.split(',');
      for (var i = 0; i < aperm.length; i++) {
        if (row[aperm[i]] != 1) return cb(false);
      }
      return cb(true);
  });
}

Calls the callback with true if the user has all of the permissions, otherwise calls the callback with false.

如果用户拥有所有权限,则使用 true 调用回调,否则使用 false 调用回调。

回答by crowder

The responses here that use FB.Data.queryare to be avoided as this point, as Facebook has deprecated FB.Data.queryentirely. The FB.api(/me/permissions ...) solution seems best.

FB.Data.query这一点上应避免使用此处的响应,因为 Facebook 已FB.Data.query完全弃用。该FB.api(/ ME /权限...)解决方案似乎是最好的。

回答by Matt Montag

You can also get permissions back from FB.login:

您还可以从 FB.login 获取权限:

FB.login(function(response) {
  // handle the response
}, {
  scope: 'publish_actions', 
  return_scopes: true
});

See https://developers.facebook.com/docs/reference/javascript/FB.login/v2.1#options

请参阅https://developers.facebook.com/docs/reference/javascript/FB.login/v2.1#options