Facebook sdk - share to managed pages - javascript

I have the following code to share to a facebook wall, but it doesn't give the option to share to any managed pages the user has admin rights to (i tried testing with my own account) can anyone help me with this? Also i want to post in the future according to a date set in feed if possible.
<script>
window.fbAsyncInit = function() {
FB.init({appId: '475259649152397',
channelUrl : '<?php get_theme_root();?>/inc/facebook-javascript-sdk/channel.php',
status: true,
cookie: true,
xfbml: true
});
};
function Login()
{
FB.login(function(response)
{
if (response.authResponse)
{
getUserInfo();
}
else
{
console.log('User cancelled login or did not fully authorize.');
}
},{scope: 'manage_pages'});
}
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#share_button').click(function(e){
e.preventDefault();
FB.ui({
method: 'feed',
name: 'This is the content of the "name" field.',
link: ' http://example.com/',
picture: 'http://myface.gif',
caption: 'insightful thought provoking caption.',
description: 'interresting".',
message: ''
});
});
});
</script>
It works just fine at sharing to the users wall but no option to share to the managed pages
thanks in advance!

option to share to any managed pages the user has admin rights to
Simply, give the PAGE_ID to the to parameter. Just like-
FB.ui({
method: 'feed',
name: 'This is the content of the "name" field.',
link: ' http://example.com/',
picture: 'http://myface.gif',
caption: 'insightful thought provoking caption.',
description: 'interresting".',
message: '',
to: MY_PAGE_ID
});
i want to post in the future
You can use the page access token if you want to post to your page at any later time, the page access token could be extended to never expiring. What are the Steps to getting a Long Lasting Token For Posting To a Facebook Fan Page from a Server
To get the pages managed by you, query for-
/me/accounts?fields=id,name
, you'll get an array of pages you are managing. Live Demo

Related

How to disable comments from facebook share dialog box?

Consider that a user visits an online news-website (nytimes, bbc, etc) and shares a story on facebook or twitter by commenting 'good' or 'bad' about it. My question is that, can news-websites (in this case nytimes, bbc, etc) control sharing the story by notify the user? If yes how they could do it? To further illustrate the problem, I want to update/cutomize FB.ui with ': ' in the following code. I don't know work around with this.
FB.ui(
{
method: 'share',
href: 'https://developers.facebook.com/docs/',
name: 'something here',
link: 'http://www.example.com/' + username,
caption: 'Contributions to the open-source world.',
<comment> : <'disabled'>
},
function(response) {
if (response && !response.error_code) {
alert('Posting completed.');
} else {
alert('Error while posting.');
}
}
);

How to verify Google's auth gapi.auth.signIn response?

I have a database with users and I want to let user to connect his website account with his google account. I want to be able to let user to log in with his google account too and maybe later to be able to interact with his google+ account etc.
User is logged in on the website and he initiates this process with a click on a button which does following:
// user initiates the process
$('#google-connect').on(function() {
gapi.auth.signIn({
'callback': function(data) {
if(data['status']['signed_in'])
console.log("Signed in", data, gapi.auth.getToken());
// just get some additional user's data
gapi.client.load('oauth2', 'v2', function() {
gapi.client.oauth2.userinfo.get().execute(function(resp) {
console.log("OAuth", resp);
data.userID = resp.id;
// tell server to add google account to user's account
$.post('/add/google', data, function(data) {
console.log("Connected", "data");
});
});
});
},
'clientid': GOOGLE_CLIENT_ID,
'cookiepolicy': 'single_host_origin',
'requestvisibleactions': 'http://schemas.google.com/AddActivity',
'approvalprompt':'force',
'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email'
//'scope': 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me'
});
return false;
});
// load libs
(function() {
var po = document.createElement('script');
po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/client:plusone.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(po, s);
})();
it basicly works without problem but I am not sure is this secure and how to verify the answers form google server. For example, Facebook login returns "signedRequest" and I am able to run the verification with hash_hmac (php) to verify the response.
Google answers with this on my gapi.auth.signIn() request:
access_token: "ya29.KgDiGDMeEpOEFxoAAAD2dV1eldwT_ZCcr-ODNR_LBKWbam7bOwZ0pplZ33hG3A"
authuser: "0"
client_id: "...."
code: "4/iqLG-akrpp_BGWGGx2b_RAqTSj29.AuyFPmgozMATOl05ti8ZT3bxU6v2jAI"
cookie_policy: "single_host_origin"
expires_at: "1402232030"
expires_in: "3600"
g-oauth-window: Window
g_user_cookie_policy: "single_host_origin"
id_token: "eyJhbGciOiJSUzI1NiIsImtpZCI6IjgxNDBjNWYxYzlkMGM3MzhjMWI2MzI4NTI4ZjdhYjFmNjcyZjViYTAifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwic3ViIjoiMTExNTQ2NTQxNjExMzkyOTAzMTYzIiwiYXpwIjoiOTMyNjIxNDU0NTUxLThrNW8yOXY0djEyZmN2cG1tNWFqYXZsbW9ic2x1NzQwLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiZW1haWwiOiJtaXJ6YS5jd0BnbWFpbC5jb20iLCJhdF9oYXNoIjoiTUNERVJkZjJpaUo0eUZ4ZHU1RUdpUSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJhdWQiOiI5MzI2MjE0NTQ1NTEtOGs1bzI5djR2MTJmY3ZwbW01YWphdmxtb2JzbHU3NDAuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJjX2hhc2giOiJXMXY4UUhkUndLM3FQbGhMZE1XY1Z3IiwiaWF0IjoxNDAyMjI4MTMwLCJleHAiOjE0MDIyMzIwMzB9.Fil-uV6oFdeiRrO_vHFr5oVOnzVIfa2DvneDYaFF_eo3HOdOmD2wElD5UOjXxTLcNHtxyXg-zInyB9wDn1aQaZpYTSgG3Q-PN7oXcmUECyX5UJ7Aga0xgjAH6j57XBTx_BVdeiq1xLTPSMq9J2hZ1jGIkv-1qPedng7bRVGuRgQ"
issued_at: "1402228430"
num_sessions: "1"
prompt: "consent"
response_type: "code token id_token gsession"
scope: "https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.moments.write https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/plus.profile.agerange.read https://www.googleapis.com/auth/plus.profile.language.read https://www.googleapis.com/auth/plus.circles.members.read https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.profile.emails.read"
session_state: "14f90adcf0c130936b1545ec15dbd8fe1515b4dc..0c9b"
state: ""
status: Object
token_type: "Bearer"
I did not find any information how to verify the response. There is no signature or something else except "id_token" which could be some kind of signature.
Is there a way to verify google's answer to be sure that information is correct, that there is no MITM? Or am I just worrying too much?
Yes, there is :
Have you read ID Token verification(PHP lib) ?
I guess, you could do the same in JavaScript using :
$.get("https://www.googleapis.com/oauth2/v1/tokeninfo",{
"id_token" : authResult["id_token"]},function(data){
//Handle data...
});

Facebook wall post without showing feed dialog box

I want to post on user's wall after successful login but i dont want to show the feed dialog popup to ask permission to publish. I want to publish silently. I had seen FB.api provides one way to post so but it does not render html tag like anchor tag and all. here is my code
function postToFeed(){
// calling the API ...
var obj = {
method: 'feed',
redirect_uri: 'url to website',
link: 'link to website',
picture: 'live picture url',
name: "",
caption: "",
description:""
};
function callback(response) {
document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
}
I have followed Facebook Developers code

Using Facebook Javascript SDK to post to user's friend's wall - getting valid response but post does not appear on Facebook

I'm attempting to use the Facebook Javascript SDK to post to an authenticated user's friend's wall. I get what appears to be a valid post ID in the response but the post does not appear on Facebook, and when I use the FB Graph API explorer to view the post, it simply returns false.
I'm using the FB login button with "publish_stream" permission for authentication and have a test FB app set up to get a valid App ID. I'm using the following code to post to the user's friend's wall:
FB.api('/[USER_ID]/feed', 'post', {
message: 'Testing the Facebook JavaScript API',
link: 'http://developers.facebook.com'
}, function(response) {
if (!response || response.error) {
console.log('Error occured');
} else {
console.log('Post ID: ' + response.id);
console.dir(response);
}
});
It works as expected when I replace [USER_ID] with 'me' - I can see the post on my FB timeline. However, when I use one of my friends' user IDs, I get a post ID response, but the post does not appear anywhere on their feed. Thoughts?
Here's my login button, too:
<fb:login-button show-faces="false" width="200" scope="publish_stream,publish_actions" perms="publish_stream"></fb:login-button>
I had the same problem. I worked on it like crazy for 5 hours. And actually, I think there is no problem. The only thing is that the posts on other's walls can arrive few hours later.
If anyone has the same problem, at least wait for a few hours before completely changing your code and turning crazy.
You can't post to a friend's wall
I was able to resolve this issue by creating an entirely new FB App from scratch, after which point the above code worked, which led me to think the issue must be in the configuration of my previous Facebook app. The only major difference I found was in App > Settings > Basic > Website > Site URL, I had entered the domain for my app and not the full path to the app page itself. So, you can indeed dynamically publish a post to the authenticated user's friend's wall.
here is with javascript sdk and facebbok c# sdk:
function fb_publish() {
FB.ui(
{
method: 'stream.publish',
message: 'Message here.',
attachment: {
name: 'Name here',
caption: 'Caption here.',
description: (
'description here'
),
href: 'url here'
},
action_links: [
{ text: 'Code', href: 'action url here' }
],
user_prompt_message: 'Personal message here'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
}
and
var client = new FacebookClient("my_access_token");
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "http://www.example.com/article.html";
parameters.picture = "http://www.example.com/article-thumbnail.jpg";
parameters.name = "Article Title";
parameters.caption = "Caption for the link";
parameters.description = "Longer description of the link";
parameters.actions = new {
name = "View on Zombo",
link = "http://www.zombo.com",
};
parameters.privacy = new {
value = "ALL_FRIENDS",
};
parameters.targeting = new {
countries = "US",
regions = "6,53",
locales = "6",
};
dynamic result = client.Post("me/feed", parameters);
and would you please mark it as answered if it helps :)

Facebook FB.ui stream.publish error

Working on a project that has a website and a Facebook page. They both reference the same SWF file and JS code that the SWF file uses to interact with the website/Facebook.
The custom Javascript action, which invokes the FB.ui() method for sharing, works so much in that the dialog/popup appears, however Facebook results with an error ("An error occurred. Please try again later."). I get this error in all browsers.
Using a stream.share method works fine, however the stream.publish is giving me grief on the website. What's notable is that the exact same code works within Facebook.
I am loading the FBJS SDK through the same methods on both sites (The Facebook page is an iframe that's hosted on the same server) and also loading the scripts for the pages in the same order.
function connectFacebook(score) {
// No score, share link
if ( score == 0 ) {
FB.ui({
method: 'stream.share',
u: 'http://www.example.com/'
});
// Has score, publish to wall
} else {
FB.ui({
method: 'stream.publish',
message: 'I scored '+score+' at Game!',
attachment: {
name: 'Game',
caption: 'Game caption',
description: 'I scored '+score+'! Play and share to win the prize pack!',
href: 'http://www.example.com/'
},
action_links: [
{ text: 'Game', href: 'http://www.example.com/' }
],
user_message_prompt: 'Tell your friends about Game'
},
function(response) {
if ( response && response.post_id ) {
//alert( 'Post was published.' );
} else {
//alert( 'Post wasn\'t published.' );
}
});
}
}
I found out what the error was. When connecting to the FBJS SDK on the website, I had a Page ID entered instead of an App ID.

Categories