<script>
function postToWall() {
var params = {};
params['message'] = 'hello visit lol';
params['name'] = 'cazzo frega';
params['link'] = 'http://apps.facebook.com/xxxxxxxxxxxxxxxx/';
params['picture'] = 'http://2.gravatar.com/avatar/7da5bead8xxxxxxxxxxxxxx75bac';
params['description'] = 'test di frase';
FB.api('/me/feed', 'post', params, function(response) {
if (!response || response.error) {
alert('lol');
} else {
alert('ok');
}
});
}
</script>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId:xxxxxxxxxxxxxxx, cookie:true,
status:true, xfbml:true
});
</script>
<a href="javascript:postToWall()">click</a>
在Facebook的墙上张贴一条消息,显示框对话框。 但是,如果我想让它自动进行,只需点击一下? 我试过whit body onload =“javascript:fucntion();但是得到一个错误......你有什么想法吗?
答案 0 :(得分:3)
如果他授权您的Facebook开发者App在他的墙上发布,则可以发布到任何用户墙。
用户可以在登录您的应用程序时授予访问权限(范围:Publish_stream)。您必须在此处指定权限。示例代码如下。
<div id="fb-root"></div>
<script type="text/javascript>
var appid = $('#FB_appId').val();
var appsecret = $('#FB_secretkey').val();
window.fbAsyncInit = function () {
FB.init({
appId: appid, // App ID
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
};
// Load the SDK Asynchronously
(function (d) {
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) { return; }
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
} (document));
FB.login(function (response) {
if (response.authResponse) {
alert(response.authResponse.userID);
} else {
console.log('User cancelled authorization');
}
}, { scope: 'publish_stream' });
</script>
在这里,您可以获得已登录用户的userId。
然后你必须通过传递applicationId和applicationsecretkey来获取AppLication accesstoken,如下所示:
string accesstoken = new System.Net.WebClient().DownloadString("https://graph.facebook.com/oauth/access_token?client_id= fbappId &client_secret= fbsecretkey &grant_type=client_credentials");
现在你有四个参数
使用这些参数,您可以按如下方式发布到他的墙上:
$.ajax({
type: 'POST',
url: 'https://graph.facebook.com/' + fb_userid + '/feed?method=post',
data: {
access_token: fb_accesstoken,
message: 'Hello world! My first post'
},
success: function (data) {
alert(data.Id);
},
error: function (data) {
alert('Failed to post to facebook');
},
dataType: "jsonp"
});
成功功能返回Facebook帖子ID。
答案 1 :(得分:0)
您需要确保要求“stream_publish”权限,如果您已获得该权限,则可以使用上述代码自动发布。没有许可,它会弹出蓝色对话框。
答案 2 :(得分:0)
我猜在通过javascript使用FB.ui()事件时仍然会有弹出对话框。您可以尝试通过PHP通过图形API发布内容。首先获取stream_publish权限并使用它来执行AJAX调用。
您可以查看此示例:http://www.facebook.com/pages/Helper-Component-Community/222933037808340?sk=app_412923142052609
答案 3 :(得分:0)
你需要把
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
在上面:).....................