Facebook api graph me - Post to Stream

时间:2013-04-11 06:16:17

标签: php facebook facebook-graph-api

enter image description here

我正在尝试允许我的Facebook应用使用下面的代码发布用户流。 底部的代码是从我获得代码但不在我的代码的网站上运行的。 工作地点:http://www.fbrell.com/fb.api/graph-me

在第二次大胆之后看到非常底线的代码:

我在下面的代码中遗漏了什么?

    visible     //facebook application id, secret key here
    $fbconfig['appid' ] = "MY###";

    $fbconfig['secret'] = "MY###";

    //set application urls here
    $fbconfig['baseUrl']    =   "MY###";
    if($id){
    $fbconfig['appBaseUrl'] =   "http://apps.facebook.com/MY###/index.php?id=".$id;
    }
    else
    {
        $fbconfig['appBaseUrl'] =   "http://apps.facebook.com/MY###/";
    }


    $uid            =   null; //facebook user id

    try{
        include_once "facebook.php";
    }
    catch(Exception $o){
        echo '<pre>';
        print_r($o);
        echo '</pre>';
    }
    // Create our Application instance.
    $facebook = new Facebook(array(
      'appId'  => $fbconfig['appid'],
      'secret' => $fbconfig['secret'],
      'cookie' => true,
    ));

    //Facebook Authentication part
     $user= $facebook->getUser();
    $loginUrl = $facebook->getLoginUrl(
            array(
            'scope'   =>  'email,publish_actions,publish_stream,status_update,user_work_history,user_location,user_birthday,user_religion_politics', 
            'redirect_uri'  => $fbconfig['appBaseUrl'],            )
    );


    $fbme = null;

    if (!$user) {
        echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
        exit;
    }
    else {
        try {
            $uid      =   $facebook->getUser();
            $fbme     =   $facebook->api('/'.$uid); 
            $friends    = $facebook->api('/' . $facebook->getUser() . '/friends');
            //$gender   = $facebook->api('/' . $facebook->getUser() . '/gender');
            $location = $fbme[location][name];
            $gender = $fbme[gender];




            /////foreach ($friends["data"] as $value) 
                ////{
                //echo '<li>';
               // echo '<div class="pic">';
               // echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
                //echo '</div>';
               // echo '<div class="picName">'.$value["name"].'</div>'; 
               // echo '</li>';

                 $NameID .= $value["name"]."|".$value["id"].",";

这是以下不起作用的代码

<fb:login-button scope="read_stream,publish_stream">
  Grant Permissions to make more examples work
</fb:login-button>

<button onclick="doPost()">Post to Stream</button>

<script>
function userData() {
  FB.api('/me', Log.info.bind('/me callback'));
  FB.api('/me/friends', Log.info.bind('/me/friends callback'));
  FB.api('/me/feed', Log.info.bind('/me/feed callback'));
  FB.api(
    '/platform/feed',
    { limit: 3 },
    Log.info.bind('/platform/feed limit: 3 callback')
  );
}
function publicData() {
  FB.api('/platform', Log.info.bind('/platform callback'));
  FB.api('/britneyspears', Log.info.bind('/britneyspears callback'));
  FB.api('/this-will-error', Log.info.bind('/this-will-error callback'));
}
window.doPost = function() {
  FB.api(
    '/me/feed',
    'post',
    { message: 'Trying the Graph' },
    Log.info.bind('/me/feed POST callback')
  );
};

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    userData();
  }
  publicData();
});
</script>

///////////////////////////////////// ///////////////////////// 更新..完整代码:

<?






/////////////////////////////////////////////
/////////////////////////////////////////////

    //facebook application id, secret key here
    $fbconfig['appid' ] = "NADA";

    $fbconfig['secret'] = "NADA";

    //set application urls here
    $fbconfig['baseUrl']    =   "NADA";
    if($id){
    $fbconfig['appBaseUrl'] =   "NADA/index.php?id=".$id;
    }
    else
    {
        $fbconfig['appBaseUrl'] =   "http://apps.facebook.com/NADA/";
    }


    $uid            =   null; //facebook user id

    try{
        include_once "facebook.php";
    }
    catch(Exception $o){
        echo '<pre>';
        print_r($o);
        echo '</pre>';
    }
    // Create our Application instance.
    $facebook = new Facebook(array(
      'appId'  => $fbconfig['appid'],
      'secret' => $fbconfig['secret'],
      'cookie' => true,
    ));

    //Facebook Authentication part
     $user= $facebook->getUser();
    $loginUrl = $facebook->getLoginUrl(
            array(
            'canvas' => 1,
             'scope'  => 'publish_stream,email,user_about_me,user_birthday,user_website',
            'scope'   =>  'read_stream,publish_stream,status_update,user_work_history,user_location,user_birthday,user_religion_politics,email', 
            'fbconnect' => 1,
            'redirect_uri' => 'https://apps.facebook.com/vizzytesting',         )
    );


    $fbme = null;

    if (!$user) {
        echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
        exit;
    }
    else {
        try {
            $uid      =   $facebook->getUser();
            $fbme     =   $facebook->api('/'.$uid); 
            $friends    = $facebook->api('/' . $facebook->getUser() . '/friends');
            //$gender   = $facebook->api('/' . $facebook->getUser() . '/gender');
            $location = $fbme[location][name];
            $gender = $fbme[gender];




            /////foreach ($friends["data"] as $value) 
                ////{
                //echo '<li>';
               // echo '<div class="pic">';
               // echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
                //echo '</div>';
               // echo '<div class="picName">'.$value["name"].'</div>'; 
               // echo '</li>';

                 $NameID .= $value["name"]."|".$value["id"].",";

                /// }           



              {?>

<style type="text/css">
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
    background-image: url(../RosaryPromotion/images/Top.jpg);
}
.Top {
    background-image: url(../RosaryPromotion/images/Top.jpg);
    display: block;
    height: 333px;
}
#Top {
    background-image: url(../RosaryPromotion/images/White.png);
    height: 370px;
    width: 885px;
}
#Pic {
    width: 90%;
    border-top-style: none;
    border-right-style: none;
    border-bottom-style: none;
    border-left-style: none;
    position: relative;
}
.ProfilePic {
    background-image: url(http://graph.facebook.com/<?=$facebook->getUser();?>/picture?type=large);
    background-repeat: no-repeat;
    background-position: center center;
    position: absolute;
    top: -6px;
    left: -21px;
    display: block;
}



#NameTag {
    background-image: url(../RosaryPromotion/images/BlueButton.png);
    background-repeat: no-repeat;
    display: block;
    height: 53px;
    width: 257px;
    position: absolute;
    top: 121px;
    line-height: 45px;
    font-family: "Arial Black", Gadget, sans-serif;
    color: #FFF;
    padding-left: 20px;
    font-size: 18px;
    left: -32px;
}

#RegisteredTimes {
    background-image: url(../RosaryPromotion/images/BlueButton.png);
    background-repeat: no-repeat;
    display: block;
    height: 53px;
    width: 257px;
    position: absolute;
    top: 215px;
    line-height: 45px;
    font-family: "Arial Black", Gadget, sans-serif;
    color: #FFF;
    padding-left: 20px;
    font-size: 18px;
    left: -31px;
}
</style>

              <table width="878" border="0" align="center" cellpadding="0" cellspacing="0" id="Top">
                <tr>
                  <th width="878" align="center" valign="top" scope="col">

<div align="left" id="Pic">



        <div class="ProfilePic" id="ProfilePic"><img src="../RosaryPromotion/images/Photo.png" width="228" height="224" alt="PhotoFrame" /></div>

   <div class="NameTag" id="NameTag"><?="$fbme[email]";?></div>
   <div class="RegisteredTimes" id="RegisteredTimes"><?="$fbme[gender]";?></div>



                    </div>
                    <?  echo count(explode(",",$NameID));  echo "<br>"; echo $fbme[location][name];
                echo "<br>";
                echo $fbme[birthday];
                echo "<br>";
                echo $fbme[gender];
                echo "<br>";
                echo $fbme[religion];?>

<fb:login-button scope="read_stream,publish_stream">
  Grant Permissions to make more examples work
</fb:login-button>

<button onclick="doPost()">Post to Stream</button>

<script>
FB.init({
appId  : '144220342416564',
frictionlessRequests: true,
status: true, 
xfbml: true,
oauth: true
});


function fbLoginStatus()
{
FB.getLoginStatus(function(response) {
    console.log(response);
    if (response.status === 'connected') {
        access_token =   FB.getAuthResponse()['accessToken'];
        doPost();
    } else {
        fblogin();
    }
 });
}


function fblogin()
{
FB.login(function(response) {
    if (response.authResponse) {
        console.log(response);
         access_token =   FB.getAuthResponse()['accessToken'];
         doPost();
    } else {
        console.log('User cancelled login or did not fully authorize.');
    }
}, {scope: 'publish_stream'});
}

function doPost(){

 var obj = {
            method: 'feed',
            link: 'http://example.com',
            picture:'http://suvudu.com/files/mt-files/Edward%20Cullen.jpg',
            name:'title',
            caption: 'Caption',
            description:'Description'
            };
console.log(obj);
function callback(response) {
        document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
    }

FB.ui(obj, callback);
}
</script>

                    </th>
                </tr>
              </table>
              <p>
                <? }

        } catch (FacebookApiException $e) {
            echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
            exit;
        }
    }
     /*$uid=$user;
    function d($d){
        echo '<pre>';
        print_r($d);
        echo '</pre>';

    }

    //FQL function
    function fqlQuery($q){
    global $facebook;
    $param  =   array(
        'method'    => 'fql.query',
        'query'     => $q,
        'callback'  => ''
    );
    //return $fqlResult   =  $facebook->api($param);
}*/
include("rosaryDatabase.php");
?>
</p>

2 个答案:

答案 0 :(得分:1)

试试这个,把这段代码放到你的doPost函数中,你必须拥有publish_stream权限。 “将<div id="fb-root"></div>放在html的正文中,将所有下面的代码放在页面”

的脚本标记中
// Load the SDK asynchronously
(function(d, s, id){
 var js, fjs = d.getElementsByTagName(s)[0];
 if (d.getElementById(id)) {return;}
 js = d.createElement(s); js.id = id;
 js.src = "//connect.facebook.net/en_US/all.js";
 fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'fb-root'));

FB.init({
appId  : 'xxxxxxxxxxxxxxxx',
frictionlessRequests: true,
status: true, 
xfbml: true,
oauth: true
});


function fbLoginStatus()
{
FB.getLoginStatus(function(response) {
    console.log(response);
    if (response.status === 'connected') {
        access_token =   FB.getAuthResponse()['accessToken'];
        doPost();
    } else {
        fblogin();
    }
 });
}


function fblogin()
{
FB.login(function(response) {
    if (response.authResponse) {
        console.log(response);
         access_token =   FB.getAuthResponse()['accessToken'];
         doPost();
    } else {
        console.log('User cancelled login or did not fully authorize.');
    }
}, {scope: 'publish_stream'});
}

function doPost(){

 var obj = {
            method: 'feed',
            link: 'http://abc.com',
            picture:'http://suvudu.com/files/mt-files/Edward%20Cullen.jpg',
            name:'title',
            caption: 'Caption',
            description:'Description'
            };
console.log(obj);
function callback(response) {
        document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
    }

FB.ui(obj, callback);
}

答案 1 :(得分:0)

实际上你必须在developer.facebook.com中创建应用程序设置,如果你没有创建应用程序,如果你已经创建了应用程序,请检查应用程序设置中的URL