我真的在与facebook api挣扎。 A创建了一个应用程序,一个对象和一个动作,我想测试在我的流中发布,(我知道公开发布必须由facebook授权,但作为我的应用程序的管理员,我可以测试它)。但它没有工作。这是脚本:
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US"
xmlns:fb="https://www.facebook.com/2008/fbml">
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# mehdientest:http://ogp.me/ns/fb/mehdientest#">
<title>OG Tutorial App</title>
<meta property="fb:app_id" content="312683425452812" />
<meta property="og:type" content="mehdientest:Campain" />
<meta property="og:url" content="http://www.example.com/pumpkinpie.html" />
<meta property="og:title" content="Sample Campain" />
<meta property="og:description" content="Some Arbitrary String" />
<meta property="og:image" content="https://s-static.ak.fbcdn.net/images/devsite/attachment_blank.png" />
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '312683425452812', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
FB.Event.subscribe('auth.login', function(response) {
// do something with response
login();
});
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
logout();
});
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
login();
}
});
function login() {
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
alert('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
}
function publish() {
FB.api('/me/mehdientest:Create?Campain=http://www.example.com/pumpkinpie.html', 'post', function(response) {
if (!response || response.error) {
alert('msg');
}
});
}
function logout(){
FB.logout(function(response){});
}
</script>
<h3>Example</h3>
<p>
<img title="Example" src="" width="550"/>
<input type="button" value="Logout" onclick="logout();"></input>
<input type="button" value="Login" onclick="login();"></input>
<input type="button" value="Publish" onclick="publish();"></input>
<div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1"></div>
</p>
</body>
</html>
我甚至试图在head区域调用publish()函数,但这也不起作用。
有人有想法吗?
最佳, Newben
答案 0 :(得分:0)
说“它不起作用”是不够的,你应该总是写下发生的事情,以便更容易帮助你。
在任何情况下,您的代码都是一团糟,当用户已经登录时,调用 FB.login 的重点是什么?
这是您的代码的修改版本,我没有测试过,但它应该指向正确的方向。 在你玩了一下之后,如果它还没有工作,请回来解释你得到的东西。
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US"
xmlns:fb="https://www.facebook.com/2008/fbml">
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# mehdientest:http://ogp.me/ns/fb/mehdientest#">
<title>OG Tutorial App</title>
<meta property="fb:app_id" content="312683425452812" />
<meta property="og:type" content="mehdientest:Campain" />
<meta property="og:url" content="http://www.example.com/pumpkinpie.html" />
<meta property="og:title" content="Sample Campain" />
<meta property="og:description" content="Some Arbitrary String" />
<meta property="og:image" content="https://s-static.ak.fbcdn.net/images/devsite/attachment_blank.png" />
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '312683425452812', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
FB.Event.subscribe('auth.login', function(response) {
FB.api('/me', function(response2) {
alert('Good to see you, ' + response2.name + '.');
});
});
FB.Event.subscribe('auth.logout', function(response) {
alert("ba bye");
document.getElementById("publish").disabled = "disabled";
});
function loggedin() {
document.getElementById("publish").disabled = false;
}
function publish() {
FB.api('/me/mehdientest:Create?Campain=http://www.example.com/pumpkinpie.html', 'post', function(response) {
if (!response || response.error) {
alert('msg');
}
});
}
</script>
<h3>Example</h3>
<p>
<img title="Example" src="" width="550"/>
<input type="button" value="Logout" onclick="logout();"></input>
<input type="button" value="Login" onclick="login();"></input>
<input type="button" id="publish" value="Publish" onclick="publish();" disabled="disabled"></input>
<div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1"></div>
</p>
</body>
</html>