php – facebook扩展权限

更新2:

好的,通过改变它“有点”工作:

$loginUrl = $facebook->getLoginUrl(array(
           'canvas' => 1,
           'fbconnect' => 0,
           'req_perms' => 'publish_stream',
           'next' => 'http://'.$_SERVER['SERVER_NAME'].'/success.PHP',
           'cancel_url' => 'http://'.$_SERVER['SERVER_NAME'].'/cancel.PHP'
        ));

对此:

$loginUrl = $facebook->getLoginUrl(array(
           'canvas' => 1,
           'fbconnect' => 0,
           'req_perms' => 'publish_stream',
           'next' => 'http://'.$_SERVER['SERVER_NAME'].'/success.PHP',
           'cancel_url' => 'http://'.$_SERVER['SERVER_NAME'].'/cancel.PHP'
        ));
        header('Location: '.$loginUrl);

即我添加标题(‘Location:’.$loginUrl);.

页面表现得很奇怪.我必须导航到页面,登录,然后刷新页面,再次登录,然后它会要求我允许发布到页面,并最终发布到页面.

为什么我要登录两次?

更新1:

我现在有以下脚本似乎不起作用.在这种状态下,我只是想张贴到自己的墙上,但最终也想发布到朋友的墙上:

<?PHP
    /**
     *
     * copyright 2011 Facebook, Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License"); you may
     * not use this file except in compliance with the License. You may obtain
     * a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     * License for the specific language governing permissions and limitations
     * under the License.
     */


    require 'facebook.PHP';

    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
        'appId'  => '<appId removed for security reasons>',
        'secret' => '<secret removed for security reasons>',
        'cookie' => true,
    ));

    // We may or may not have this data based on a $_GET or $_COOKIE based session.
    //
    // If we get a session here, it means we found a correctly signed session using
    // the Application Secret only Facebook and the Application kNow. We dont kNow
    // if it is still valid until we make an API call using the session. A session
    // can become invalid if it has already expired (should not be getting the
    // session back in this case) or if the user logged out of Facebook.
    $session = $facebook->getSession();

    $me = null;
    // Session based API call.
    if ($session) {
        try {
            $uid = $facebook->getUser();
            $me = $facebook->api('/me');

            $post = $facebook->api("/me/Feed", "POST",  array('message' => 'Hello! I\'m using the FB Graph API!'));
        } catch (FacebookApiException $e) {
            error_log($e);
        }
    }

    // login or logout url will be needed depending on current user state.
    if ($me) {
        $logoutUrl = $facebook->getlogoutUrl();
    } else {
        $loginUrl = $facebook->getLoginUrl(array(
           'canvas' => 1,
           'fbconnect' => 0,
           'req_perms' => 'publish_stream',
           'next' => 'http://'.$_SERVER['SERVER_NAME'].'/success.PHP',
           'cancel_url' => 'http://'.$_SERVER['SERVER_NAME'].'/cancel.PHP'
        ));
    }

?>

<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
        <title>PHP-sdk</title>

        <style>
            body {
                font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
            }

            h1 a {
                text-decoration: none;
                color: #3b5998;
            }

            h1 a:hover {
                text-decoration: underline;
            }
        </style>
    </head>

    <body>
    <!--
        We use the JS SDK to provide a richer user experience. For more info,
        look here: http://github.com/facebook/connect-js
    -->
        <div id="fb-root"></div>
        <script>
        window.fbAsyncInit = function() {
            FB.init({
                appId   : '<?PHP echo $facebook->getAppId(); ?>',
                session : <?PHP echo json_encode($session); ?>, // don't refetch the session when PHP already has it
                status  : true, // check login status
                cookie  : true, // enable cookies to allow the server to access the session
                xfbml   : true // parse XFBML
            });

            // whenever the user logs in, we refresh the page
            FB.Event.subscribe('auth.login', function() {
                window.location.reload();
            });
        };

        (function() {
            var e = document.createElement('script');
            e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
            e.async = true;
            document.getElementById('fb-root').appendChild(e);
        }());
    </script>


    <h1><a href="example.PHP">PHP-sdk</a></h1>

    <?PHP if ($me): ?>
        <a href="<?PHP echo $logoutUrl; ?>">
            <img src="http://static.ak.fbcdn.net/rsrc.PHP/z2Y31/hash/cxrz4k7j.gif">
        </a>
    <?PHP else: ?>
        <div>
            Using JavaScript &amp; XFBML: <fb:login-button></fb:login-button>
        </div>
    <?PHP endif ?>

    <h3>Session</h3>
    <?PHP if ($me): ?>
        <pre><?PHP print_r($session); ?></pre>

        <h3>You</h3>
        <img src="https://graph.facebook.com/<?PHP echo $uid; ?>/picture">
        <?PHP echo $me['name']; ?>

        <h3>Your User Object</h3>
        <pre><?PHP print_r($me); ?></pre>
    <?PHP else: ?>
        <strong><em>You are not Connected.</em></strong>
    <?PHP endif ?>
  </body>
</html>

我收到以下错误

[Wed Apr 27 22:28:16 2011] [error] [client <ip address removed for security reasons>] OAuthException: (#200) The user hasn't authorized the application to perform this action, referer: http://<ip address removed for security reasons>/index.PHP

原始问卷:

我有以下工作脚本,允许有人使用他们的Facebook详细信息登录我的页面,然后我可以捕获他们的access_token,所以我可以使用它与图形api:

<?PHP
    /**
     *
     * copyright 2011 Facebook, Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License"); you may
     * not use this file except in compliance with the License. You may obtain
     * a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     * License for the specific language governing permissions and limitations
     * under the License.
     */


    require 'facebook.PHP';

    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
        'appId'  => 'app id goes here',
        'secret' => 'secret id goes here',
        'cookie' => true,
    ));

    // We may or may not have this data based on a $_GET or $_COOKIE based session.
    //
    // If we get a session here, it means we found a correctly signed session using
    // the Application Secret only Facebook and the Application kNow. We dont kNow
    // if it is still valid until we make an API call using the session. A session
    // can become invalid if it has already expired (should not be getting the
    // session back in this case) or if the user logged out of Facebook.
    $session = $facebook->getSession();

    $me = null;
    // Session based API call.
    if ($session) {
        try {
            $uid = $facebook->getUser();
            $me = $facebook->api('/me');
        } catch (FacebookApiException $e) {
            error_log($e);
        }
    }

    // login or logout url will be needed depending on current user state.
    if ($me) {
        $logoutUrl = $facebook->getlogoutUrl();
    } else {
        $loginUrl = $facebook->getLoginUrl();
    }

?>

<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
        <title>PHP-sdk</title>

        <style>
            body {
                font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
            }

            h1 a {
                text-decoration: none;
                color: #3b5998;
            }

            h1 a:hover {
                text-decoration: underline;
            }
        </style>
    </head>

    <body>
    <!--
        We use the JS SDK to provide a richer user experience. For more info,
        look here: http://github.com/facebook/connect-js
    -->
        <div id="fb-root"></div>
        <script>
        window.fbAsyncInit = function() {
            FB.init({
                appId   : '<?PHP echo $facebook->getAppId(); ?>',
                session : <?PHP echo json_encode($session); ?>, // don't refetch the session when PHP already has it
                status  : true, // check login status
                cookie  : true, // enable cookies to allow the server to access the session
                xfbml   : true // parse XFBML
            });

            // whenever the user logs in, we refresh the page
            FB.Event.subscribe('auth.login', function() {
                window.location.reload();
            });
        };

        (function() {
            var e = document.createElement('script');
            e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
            e.async = true;
            document.getElementById('fb-root').appendChild(e);
        }());
    </script>


    <h1><a href="example.PHP">PHP-sdk</a></h1>

    <?PHP if ($me): ?>
        <a href="<?PHP echo $logoutUrl; ?>">
            <img src="http://static.ak.fbcdn.net/rsrc.PHP/z2Y31/hash/cxrz4k7j.gif">
        </a>
    <?PHP else: ?>
        <div>
            Using JavaScript &amp; XFBML: <fb:login-button></fb:login-button>
        </div>
    <?PHP endif ?>

    <h3>Session</h3>
    <?PHP if ($me): ?>
        <pre><?PHP print_r($session); ?></pre>

        <h3>You</h3>
        <img src="https://graph.facebook.com/<?PHP echo $uid; ?>/picture">
        <?PHP echo $me['name']; ?>

        <h3>Your User Object</h3>
        <pre><?PHP print_r($me); ?></pre>
    <?PHP else: ?>
        <strong><em>You are not Connected.</em></strong>
    <?PHP endif ?>
  </body>
</html>

用户登录后,我了解到我可以通过以下方式获取他们的朋友列表:

https://graph.facebook.com/me/friends?access_token=...

我无法弄清楚如何使用扩展权限,所以我的应用程序可以发布给用户朋友的Facebook墙.

显然我应该使用扩展的权限加上以下内容

curl -F 'access_token=...' \
     -F 'message=Hello, Arjun. I like this new API.' \
     https://graph.facebook.com/arjun/Feed

我不明白我应该如何从PHP做到这一点.

解决方法:

更新:

好吧,我自己无法真正测试它,所以只需要一些建议就可以尝试.将$loginUrl更改为:

$loginUrl = $facebook->getLoginUrl(array(
    'req_perms' => 'publish_stream',
    'next' => 'http://'.$_SERVER['SERVER_NAME'].'/success.PHP',
    'cancel_url' => 'http://'.$_SERVER['SERVER_NAME'].'/cancel.PHP'
));

在整个上下文中,文件的顶部应如下所示:

require 'facebook.PHP';

$facebook = new Facebook(array(
    'appId' => '<appId removed for security reasons>',
    'secret' => '<secret removed for security reasons>',
    'cookie' => true,
));

$session = $facebook->getSession();

$me = null;
if ($session)
{
   try
   {
       $uid = $facebook->getUser();
       $me = $facebook->api('/me');

       $post = $facebook->api("/me/Feed", "POST", array('message' => 'Hello! I\'m using the FB Graph API!'));
   }
   catch (FacebookApiException $e)
   {
      error_log($e);
   }
}
else
{
$loginUrl = $facebook->getLoginUrl(array(
        'req_perms' => 'publish_stream',
        'next' => 'http://' . $_SERVER['SERVER_NAME'] . '/success.PHP',
        'cancel_url' => 'http://' . $_SERVER['SERVER_NAME'] . '/cancel.PHP'
   ));
   header('Location: ' . $loginUrl);
}

那么,首先检查您是否有会话,因此您需要像示例中那样配置Facebook SDK:

$facebook = new Facebook(array(
    'appId'  => 'app id goes here',
    'secret' => 'secret id goes here',
    'cookie' => true,
));

然后,您可以检查用户是否已登录并且您的应用已获得授权:

if ($facebook->getSession() == null) {
   // not logged in or not authorized
}

在if子句中,您必须重定向到正确的login-url以获得所需的所有权限:

$loginUrl = $facebook->getLoginUrl(array(
   'canvas' => 1,
   'fbconnect' => 0,
   'req_perms' => 'publish_stream',
   'next' => // url where to go when you were authorized
   'cancel_url' => // url to go to when user cancelled
));
header('Location: '.$loginUrl);

获得权限后,您可以使用在文档中提到的发布

$facebook->api(/* url */, array(/* additional parameters go here */));

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...