简体   繁体   中英

Post comments to someones profile post or page post or group post using facebook javascript sdk

I want to post comments to someone's Facebook profile post or page post or group post using the Facebook Javascript SDK with an access token and Graph API .

I can generate an application access token from a current logged in user with publish_stream permission. How do I use the Javascript SDK call using post id so that a user can comment from outside Facebook, that is from my site where all page/profile/group posts are shown?

I tried the PHP SDK, but is not working on the server site.

Assuming user already logged in and access token has publish_stream permissions, you should call

FB.api('/'+ _POST_ID_HERE_ +'/comments', 'POST', { message:"Your comment text" },
       function(response)
       {
            if (response && !response.error && response.id)
            {
                 alert('New comment id = '+response.id);
            }
       });

function Comment_Clicked(pid) { FB.init({ appId : '472562079456084', status : true, // check login status //channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File cookie : true, // enable cookies to allow the server to access the session xfbml : true // parse XFBML //oauth:true });

    FB.login(function(response) {
        if (response.status=='connected') {
            if (response.authResponse.accessToken) {
                var token = response.authResponse.accessToken;      

                FB.api('/'+pid+'/comments?access_token='+token+'', 'POST',{ message:'test' }, function(response) {
                  if (!response || response.error) {
                    ms_js("#msfb-error-comlike").text("Errors you may not have permissions ");
                  } else {
                    ms_js("#msfb-error-comlike").text("Post success");
                  }
                });
                //window.location = "<? echo $absolute_url; ?>&temptoken="+ token +"";
            } else {
                // user is logged in, but did not grant any permissions
                alert('You can create an access token only for your own profiles and pages.');
            }
        } else {
            // user is not logged in
            alert('To use fb.wall you have to create an access token.');
        }
    }, {scope:'read_stream,publish_stream'}); //exclude publish_stream,publish_actions  

}

this my complete function which does the comment.here i am generating a access token by app id.is it right? by a app access token a user can comment on another page,profile post instead of his page or profile post?! things go ok so is using. @Const actually publish_stream permission works for another user performing comment to another user profile post which i tested.but for the user for which problem is still problem, dont know why!.generating token giving all permissions using javascript sdk in my local site not working but works when i generate here - developers.facebook.com/tools/explorer....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM