I am loading the api like so:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelURL : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
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/el_GR/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
I am opening a page through a jQuery UI dialog and reparsing the #comments section with:
<script>FB.XFBML.parse(document.getElementById('comments'));</script>
While on the non-ajax loaded page works fine, when I am opening it from the dialog it gives the error (on the js console):
Permission denied to access property 'fb_xdm_frame_https' # https://s-static.ak.fbcdn.net/rsrc.php/v1/yt/r/Via9RsGlPnX.js:66
and it looks as if it still is loading (the gif keeps playing)
Any ideas?
I think you are not loading javascript sdk properly. Try this code
window.fbAsyncInit = function() {
FB.init({
appId : 'appid', // App ID
channelUrl : 'http://www.url.com', // Channel File
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', 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));
Related
First, a facebook user is logged client-side with using Facebook JS SDK.
FB.init() is called with the following parameters:
{
version : 'v2.0',
appId: '...',
status : true,
cookie : true,
xfbml : false
}
Then, in PHP, a session is created:
$session = (new FacebookJavaScriptLoginHelper())->getSession();
Once FB API calls have been made from PHP, I still can continue calling FB API from withing js.
But as soon as my page sends a new command to the server instructing it to make an FB API call, it doesn't work the second time, and the FB API says:
An access token is required to request this resource.
Obviously, this is called one more time and after that it stops working:
$session = (new FacebookJavaScriptLoginHelper())->getSession();
If the user updates the page in his browser, the next call from the server will work again. What I want instead is to keep querying FB API from both the server and the browser simultaneously without reloading the page.
It is out of date, but maybe will help someone
<?php
//some code here - requires and uses
FacebookSession::setDefaultApplication($SETTINGS['fbappid'], $SETTINGS['fbsecret']);
$helper = new FacebookJavaScriptLoginHelper();
$session;
try {
$session = $helper->getSession();
} catch(FacebookRequestException $ex) {
//
} catch(\Exception $ex) {
//
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $SETTINGS['fbappid'] ?>',
xfbml : true,
version : 'v2.0',
status : true,
cookie : true
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
FB.login(function () {
FB.api('/me', {
fields: 'id'
}, function (response) {
console.log(response);
});
}, {
scope: 'publish_actions'
});
} else if (response.status === 'not_authorized') {
} else {
}
});
};
(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/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div id="fb-root"></div>
<?php
//here url to go to my app
echo "<a href='...'>Sign in to Facebook application</a>";
if ($session) {
echo "<br/>logged in";
}
?>
</body>
</html>
Note that login comes after second access to page if cookies were not set. So ajax request is needed to check properly.
I have a multilingual ember application in which i've included the facebook sdk (async):
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelURL : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
// Additional initialization code here
};
// Load the SDK Asynchronously
(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));
When the user change the application's language, i would like to change the fb sdk language as well and reparse every fb tag to get everything translated;
I've made a function to set the js.src attribute properly, but the sdk is loaded only when the page refresh, and it happens only on the first load.
How can i reload the sdk with the properly js.src attribute in ember at runtime?
I'm trying to load some posts asynchronously via the javascript SDK from facebook. I'm loading the javascript files at the bottom of the page:
<html>
<head>.....</head>
<body>
.....
.....
.....
<div id="fb-root"></div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="/res/jquery.stp.js" charset="utf-8"></script>
</body></html>
In jquery.stp.js I'm loading these things in this order:
- the javascript sdk
- some jquery-things
- a function loadFBPosts()
- fb.init()
The file:
var isLoaded = false;
(function(d, debug){
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" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));
$(function(){
//some jquery event things
})
function loadFBPosts(fbPageId) {
// Facebook call
if(isLoaded) {
//do some things with the FB.api()
//and catch errors...
}else{
//api not loaded...
}
}
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : 'xxxxx',
channelUrl : '//lab.example.com/channel.php',
status : true,
cookie : true,
xfbml : true
});
isLoaded = true;
laadFBPosts(); // here after FB.init is initialized call the function.
};
Questions
Now something odd happens. If I run this page an error pops up: "An access token is required to request this resource" with error-code 104.
But if I click a link on the page <a href='javascript:loadFBPosts()'>click</a> then the function gets execute like it should.
How is this possible? The resources I try to get with the FB.api() are from public facebook pages. eg FB.api('/218818688131271/posts?limit=5') so I shouldn't need an access token. Right?
If I do need an acces token: how do I obtain one if I don't want to bother every visitor with a login screen; because it's just a displaying of public info....
The only thing I want to do is to display the last 5 posts off my page and the upcoming events.
I have a Facebook page. I want to know if a person has clicked on like button or not and do something in subscribe.event 'create-edge' callback.
This works fine if a person is already logged into Facebook. The 'create-edge' event is getting fired. However, if a person is not logged in, this event is not getting fired.
I don't have any appId, so I'm sending it nothing.
Code for your reference:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.me/ns/fb#" >
<head>
<meta charset="utf-8">
<title>Facebook Like Button</title>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '', // App ID
channelURL : '', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : false, // enable OAuth 2.0
xfbml : true // parse XFBML
});
FB.Event.subscribe('edge.create', function(response) {
alert("say something");
//my code
});
FB.Event.subscribe('auth.login', function(response) {
alert('The status of the session is: ' + response.status);
});
};
// Load the SDK Asynchronously
(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));
</script>
<fb:like href="http://www.facebook.com/My_Page" send="false" layout="button_count" width="450" show_faces="false"></fb:like>
</body>
</html>
And also i am getting this error:
FB.getLoginStatus() called before calling FB.init().
Seems like I am getting it because I don't have any appId to send in FB.init.
What is the cause of this?
This happens if the user has already liked your page, and is not logged in. To fix this, I had to put FB.init() in a loop.. like this:
setInterval(function()
{
FB.init({
appId : 'app_id'
channelUrl : 'channel_url'
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : false // parse XFBML
});
}, 5000);
SO I am trying to have my web site publish info to a user's facebook page. Where I am now is that I placed the following code (as instructed by another stackoverflow contributor), which is getting executed however the alert with "error occurred" comes up. Do I need to request the publish_stream permission as well? If so, I cannot figure out how to add that to my code. Sorry, I'm not much of a scripter and I have very limited knowledge on doing any integration into facebook. My ultimate goal is to be able to post information to a users facebook page on their behalf (this won't be done without their permission). Any help would be much appreciated:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXXXXXXXXXXXXXXXXX', // App ID
channelUrl : '//www.XXXXXXXXXXXXXX.com/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true
});
});
// Additional initialization code here
};
// 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));
</script>
<SCRIPT language="JavaScript">
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
</SCRIPT>
<SCRIPT language="JavaScript">
var body = 'Reading JS SDK documentation';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
})
</SCRIPT>
Why don't you expand the error handling a little bit?
alert("Error occured: " + response.error.message);
And did you check the console log for errors? Maybe authentication didn't work.