0

我知道这个错误在这里已经被咬死了,我阅读了每一篇文章以更好地理解我的问题。

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

我正在使用 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
?>

I read in various topics that I need publish_stream permission from user to perform this action. But since I store user info separately in my own DB, how do I get this publish stream permission? Do I need to modify wordpress plugin to store some kind of access token? and utilize this token to post to wall?

4

1 回答 1

2
  1. you generate loginurl with publish_stream permission
  2. if user is not logged in with permission, you have to make sure he does
  3. redirect user to proper page

you can do it like this,

<?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); 
?>
于 2012-08-30T12:26:31.627 回答