如何获得许可?

时间:2010-10-14 16:45:38

标签: facebook

我创建了一个简单的facebook应用程序。 现在我想获得read_stream的许可。

我已经阅读了很多文档,但我不明白。

你能告诉我如何使用PHP或javascript获得此权限吗?

编辑:

我已经使用过这个源代码(你可以使用它here)但是没有用,因为我只获得了基本权限:

<?php

require_once('facebook.php');

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


$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) {
 $loginUrl = $facebook->getLoginUrl();


} else {

  // $fbme is valid i.e. user can access our app
  $logoutUrl = $facebook->getLogoutUrl();
}   


// This call will always work since we are fetching public data.


?>
<!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
        });


FB.login(function(response) {
  if (response.session) {
    if (response.perms) {
      // user is logged in and granted some permissions.
      // perms is a comma separated list of granted permissions
    } else {
      // user is logged in, but did not grant any permissions
    }
  } else {
    // user is not logged in
  }
}, {perms:'read_stream'});


        // 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>I love you</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>

2 个答案:

答案 0 :(得分:1)

   var cb = function(response) {
                //Log.info('FB.login callback', response);
   if (response.status === 'connected') {
   //Log.info('User logged in');
   } else {
   //Log.info('User is logged out');
   }
   };
              FB.login(cb, {
                scope: 'publish_stream',
                enable_profile_selector: 1
              });

参考:

http://www.fbrell.com/saved/ac4c4abe0ea46d6363350b6f0c12c6ea http://www.softwareandfinance.com/apps/facebook/graph_api_access_feed_publish.html

//服务器端验证

$permissions = $facebook->api("/me/permissions");

        if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
            $result = $facebook->api($feed_dir, 'post', $msg_body);
        }

// result is id of post

答案 1 :(得分:0)

在javascript API中,您可以使用FB.login()在登录期间请求权限。更多关于它here(附例子)

在PHP中,您可以在身份验证过程中请求扩展权限。关于它的更多信息here(参见“请求扩展权限”)

Javascript方法更容易imo。

<强>更新

好的,第三种方法是使用fb:login-button fbml标记获取权限:

<fb:login-button perms="read_stream,offline_access"></fb:login-button>