4

我正在开发与竞赛和促销相关的 Facebook 应用程序,我的目的是在我公司的页面上创建一个选项卡,以提供对该应用程序的访问。

一旦他们,用户可以提名当地公司获奖。稍后的; 提名完成后,用户可以投票选出获胜者。

我已将 Open Graph 集成到我的应用程序中,以便我可以利用对象类型 ( Organization)、操作类型 ( Nominate, Vote For) 和聚合 ( Nominations)。我的主要目标是然后将这些操作转移到用户的时间线上。

我使用recipebox 示例作为我的基础。我提供了我的代码来演示身份验证和提交操作类型/对象类型组合所需的发布操作。

<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US"> 
<head prefix="og: http://ogp.me/ns# og_<<app namespace>>: http://ogp.me/ns/apps/<<app namespace>>#"> 
    <meta property="fb:app_id" content="<<app id>>" /> 
    <meta property="og:type" content="<<app namespace>>:organization" /> 
    <meta property="og:title" content="Client 1" /> 
    <meta property="og:image" content="<<client image path>>" /> 
    <meta property="og:description" content="Client 1 description here ... " /> 
    <meta property="og:url" content="<<custom client URL>>">
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script type="text/javascript">
        // Load the SDK Asynchronously
        (function(d){
            var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
            if (d.getElementById(id)) {return;}
            js = d.createElement('script'); js.id = id; js.async = true;
            js.src = "//connect.facebook.net/en_US/all.js";
            ref.parentNode.insertBefore(js, ref);
        }(document));

        // Init the SDK upon load
        window.fbAsyncInit = function() {
            FB.init({
                appId      : '<<app id>>', // App ID
                status     : true, // check login status
                cookie     : true, // enable cookies to allow the server to access the session
                xfbml      : true  // parse XFBML
            });

            // listen for and handle auth.statusChange events
            FB.Event.subscribe('auth.statusChange', function(response) {
                if (response.authResponse) {
                    // user has auth'd your app and is logged into Facebook
                    FB.api('/me', function(me){
                        if (me.name) {
                            document.getElementById('auth-displayname').innerHTML = me.name;
                        }
                    })
                    document.getElementById('auth-loggedout').style.display = 'none';
                    document.getElementById('auth-loggedin').style.display = 'block';
                } else {
                    // user has not auth'd your app, or is not logged into Facebook
                    document.getElementById('auth-loggedout').style.display = 'block';
                    document.getElementById('auth-loggedin').style.display = 'none';
                }
            });

            // respond to clicks on the login and logout links
            document.getElementById('auth-loginlink').addEventListener('click', function(){
                FB.login();
            });
            document.getElementById('auth-logoutlink').addEventListener('click', function(){
                FB.logout();
            });
        }       

        function nominate () {
            FB.api('/me/<<app namespace>>:Nominate&organization=<<custom client URL>>', 'post',  function(response) {

                if (! response || response.error) {
                    alert('Error occured');
                    console.log(response);
                } else {
                    alert('Post was successful! Action ID: ' + response.id);
                }
            });
        }
    </script>
</head> 
<body> 
    <div id="fb-root"></div>
    <div id="auth-status">
        <div id="auth-loggedout">
            <a href="#" id="auth-loginlink">Login</a>
        </div>
        <div id="auth-loggedin" style="display:none">
            Hi, <span id="auth-displayname"></span>  
            (<a href="#" id="auth-logoutlink">logout</a>)
        </div>
    </div>
    <h2>Client 1</h2> 
    <form>
        <input type="button" value="Nominate" onclick="nominate()" />
    </form> 
    <fb:activity actions="<<app namespace>>:nominate"></fb:activity>
</body> 
</html>

测试用户遇到以下错误:

Requires extended permission: publish_actions

我遇到了以下错误(我是这个应用程序的管理员):

This method must be called with an app access_token 

第一个错误让我很困扰。我无法从中publish_actions选择Apps | <My App> | Permissions | Extended Permissions。此外,我遇到的补救措施建议我将我的应用程序重新分类为Games(这不起作用)并完成我的聚合(我做了;仍然不起作用)。

  1. 我该如何克服这个错误?
  2. 我能做些什么来修复我的 This method must be called with an app access_token错误?

提前致谢,

麦克风

编辑

我相信access_token我遇到的错误是因为我直接在页面标签 URL 上测试/与应用程序交互;不是通过脸书。

我不再收到Requires extended permission: publish_actions error; 但是,我的测试人员和开发人员是。我知道我遇到了这个错误,因为在初始 facebook.com 登录提示中没有请求 publish_actions 权限。

如果我的开发人员/测试人员退出 Facebook,请使用我的提示重新登录:

FB.login(function (response) { }, { scope:'publish_actions'});

然后将此权限和应用程序集成到他们的 Facebook 会话中。

我的最后一个问题是 - 是否有一种事实上的首选方法来请求此权限而无需登录/退出 Facebook?

4

2 回答 2

2

我已经修复了我的两个错误。感谢那些向我提供反馈并让我走上正确道路的人。

这些错误已得到解决:

1. Requires extended permission: publish_actions

2. This method must be called with an app access_token 

首先,CBroe 是对的。由于我需要扩展权限,因此(publish_actions)我需要在回调函数之后指定这是我的选项对象,FB.login()如下所示:

FB.login(function (response) {
    if (response.authResponse) { // The user has authenticated
        authenticatedSoDoSomething();
    } 
    else { // The user has not authenticated
        notAuthenticatedSoDoSomethingElse(); 
    }
}, { scope:'publish_actions' });

最后,我没有成功使用 JavaScript SDK 通过FB.api(). 这是我使用的代码

FB.api('/me/<<app namespace>>:<<my action>>&<<my object>>=<<a URL>>', 'post',
    function(response) {
        if (! response || response.error) {
            alert('Error occured');
        } else {
            alert('Post was successful! Action ID: ' + response.id);
        }
})

我忽略了包含应用程序访问令牌。应用程序访问令牌可以通过连接应用程序 ID、管道和应用程序密钥来构造。

我使用 PHP SDK 来完成这个请求,因为我想对我的 api 保密。这是我使用的 [大部分] 代码:

require_once('facebook-php-sdk/src/facebook.php');

$facebook = new Facebook(
    array(
        'appId' => '<<my app id>>', 
        'secret' => '<<my app secret>>'));

$user = $facebook->getUser();

if ($user) {
    try {
        $user_profile = $facebook->api('/me');
    } 
    catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
        echo json_encode(array('success' => false));
    }
}

try {
    $facebook->api('/' . $user . '/<<app namespace>>:<<my action>>&<<my object>>=<<a URL>>', 'POST', array('access_token' => $facebook->getAppId() . '|' . $facebook->getApiSecret()));
}
catch (FacebookApiException $e) {
    echo var_dump($e->getResult());
}

希望这可以帮助!

于 2012-09-07T17:18:43.737 回答
0

对于第二个,使用您的应用程序访问令牌进行调用,就像错误所说的那样;如果您已经这样做并且失败了,请在 facebook App Dashboard 的应用程序设置中检查该应用程序是否设置为“本机/桌面”模式。如果是,则应用访问令牌不受信任,并且 API 的行为就像您没有提供它一样。

首先,您确定用户正在向您的应用授予 publish_actions 权限吗?使用用户的访问令牌调用/me/permissions将向您显示授予了哪些权限。请注意,经过身份验证的引荐/应用中心登录流程与您的应用的常规流程是分开的,用户最终通过经过身份验证的引荐或应用中心登录到那里,因此请检查您是否包含publish_actionsscope对 Oauth 对话框的调用中。

于 2012-08-24T00:15:01.460 回答