facebook php SDK,publish_stream OAuthException:(#200)

时间:2012-08-30 12:10:40

标签: php facebook facebook-graph-api

我知道这个错误在这里被咬死了,我读了每一个错误都能更好地理解我的问题。

但我的问题有点不同,我想知道是否有人可以给我一个好的建议去哪儿看。

我使用wordpress + wordpress社交登录。此插件对用户进行身份验证,并在数据库中存储名称/年龄/电子邮件/ fbID / fbProfilePic。

我的网站上有一个小功能,用户通过Facebook注册后可以点击并将信息发布到他们的墙上。

我的代码如下所示:

<?php
//IF user is registered via facebook, when he clicks INTERESTED message will appear on his/her wall to spread the news

  $user = get_user_meta($current_user->ID,'Facebook',true);

    if ($user && $_GET['commentstab'] == 1 && !$_POST['delmycom']) {



      require ('Facebook/facebook.php');

      //Access details
      $facebook = new Facebook(array(
        'appId'  => 'XXX',
        'secret' => 'XXX'
        //'cookie' => true
      ));


          //try {
              $params = array(
                  'message'       =>  "Hurray! This works :)",
                  'name'          =>  "This is my title",
                  'caption'       =>  "My Caption",
                  'description'   =>  "Some Description...",
                  'link'          =>  "http://stackoverflow.com",
                  'picture'       =>  "http://i.imgur.com/VUBz8.png",
              );
              $post = $facebook->api("/$user/feed","POST",$params);

              echo "Your post was successfully posted to UID: $user";
          //} //try

          //catch (FacebookApiException $e) {
          //    $result = $e->getResult();
          //} //catch


    } //master if
?>

我在各种主题中读到了我需要用户执行此操作的publish_stream权限。但由于我将用户信息分别存储在自己的数据库中,如何获得此发布流权限?我是否需要修改wordpress插件来存储某种访问令牌?并利用此令牌发布到墙上?

1 个答案:

答案 0 :(得分:2)

  1. 您使用publish_stream权限生成loginurl
  2. 如果用户未经许可登录,则必须确保
  3. 将用户重定向到正确的页面
  4. 你可以这样做,

    <?php
    include_once("facebook.php"); 
        $facebook = new Facebook(array(
          'appId'  => APP_ID,
          'secret' => APP_SECRET,
          'cookie' => true,
        ));
        $user = $facebook->getUser();
    
    
    
        if ($user) {
          try {
            // Get the user profile data you have permission to view
            $user_profile = $facebook->api('/me');
          } catch (FacebookApiException $e) {
            $user = null;
          }
        } else {
            $loginUrl = $facebook->getLoginUrl(array('scope' =>'publish_stream','redirect_uri'=>'example.com'));
          die('<script> top.location.href="'.$loginUrl.'";</script>');
        }
    
        $params = array(
                      'message'       =>  "Hurray! This works :)",
                      'name'          =>  "This is my title",
                      'caption'       =>  "My Caption",
                      'description'   =>  "Some Description...",
                      'link'          =>  "http://stackoverflow.com",
                      'picture'       =>  "http://i.imgur.com/VUBz8.png",
                  );
        $post = $facebook->api("/$user/feed","POST",$params); 
    ?>