2010-10-14 98 views
0

我已经创建了一个简单的Facebook应用程序。 现在我想获得read_stream的权限。如何获得许可?

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

你能告诉我如何获得这个权限与PHP或JavaScript?

编辑:

我已经使用这个源代码(可以SE它的工作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> 

回答

0

在的JavaScript API,你可以请求权限在登录期间使用FB.login()。关于它的更多信息here(附带示例)

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

Javascript方法更容易实现。

UPDATE

好有使用FBML fb:login-button标签了权限的第三条道路:

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

请参见上面的...我,如果你使用的登录按钮FBML编辑的问题:) – xRobot 2010-10-14 17:53:16

+0

@xRobot OK,然后有越来越权限的另一种方式(看我的编辑) – serg 2010-10-14 17:59:49

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