How to unsubscribe Facebook webhook if page's token expired? - facebook

My Facebook App keep receiving webhooks from token expired pages. How can I unsubscribe them to avoid unnecessary incoming traffics?

You could use the following call to unsubscribe app from page:
`
curl -X DELETE -F "access_token=<PAGE_ACCESS_TOKEN>" "https://graph.facebook.com/v5.0/<APP_ID>/subscribed_apps"
Or through a function call:
function unSubscribeApp(page_id, page_access_token) {
console.log('Unsubscribing app from page! ' + page_id);
FB.api(
'/' + page_id + '/subscribed_apps',
'delete',
{access_token: page_access_token},
function(response) {
console.log('Successfully unsubscribed page', response);
}
);
}
`

Related

Why is there a difference in the Facebook user Id returned?

I'm using the Facebook SDK to implement login.
// Here we run a very simple test of the Graph API after login is
// successful. See statusChangeCallback() for when this call is made.
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
console.log(response);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + response.email + response.id + '!';
});
FB.api('/me/permissions', function(response) {
console.log(response);
});
}
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
The item that gets returned from response.id is not the same as my Facebook user ID when I look at the Graph Explorer here: https://developers.facebook.com/tools/explorer?method=GET&path=me%3Ffields%3Did%2Cname%2Cemail&version=v2.5.
Why is that?
You probably selected the "Graph Explorer" App in the API Explorer. Select your own app in the dropdown and you will get the same ID. It´s an "App Scoped ID". See the changelog for v2.0 to find out about those: https://developers.facebook.com/docs/apps/changelog#v2_0

Posting to a user's wall in FB with permission, possible?

is it possible to login to a website with a FB login and have the user post to his friend with permission? By "with permission" I mean the friend has to approve the post. Is this possible?
Yes it is possible. We have an application at my job that posts on the user's profile, as them, every week.
From the facebook doc (https://developers.facebook.com/docs/reference/javascript/FB.api/):
If you have an authenticated user with the publish_stream permission, and want to publish a story to their feed:
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);
}
});

Login repeats after authenticate, if i try to send a friends request

Im trying to build a 'cheer action' for my app. It is a game in iPhone, so I want to post a 'welcome message' in the users wall (I will need 'public_stream' permission) and then invite his friends to visit my webpage.
Here my question.. I authenticate the user using:
_url = "http://www.facebook.com/dialog/oauth/?client_id=" + app_id + "&redirect_uri=" + app_net_folder + auth_response_uri + "&state=" + session_id;
I get response in my uri, and then I get the access_token, user_id, etc.
Then I post a message in his wall in this way:
_url = "https://www.facebook.com/dialog/apprequests?app_id=" + app_id;
_url += "&message=" + string_ToUrl(String_ToHTML(request_msg));
Now I want to invite his friends (using a multi friend list dialog). To do it, I can only use JavaScript FB.ui. In this way:
FB.init({appId : app_id, logging : false, frictionlessRequests: true, oauth : true});
FB.ui({method: 'apprequests', message: msg, title: title}, requestCallback);
When I do it, a window is opened asking me again for the user's login.. but my user has been already authenthicated. So I dont know how to say to FB in JavaScript that my user has been alredy authenthicated! (I even have the access_token, but I don't know how to use it in JavaScript functions).
Someone knows what am I doing wrong? or how to do those two actions asking just one time for the user's login?
Thanks!!
if you are already log in, you can get login user id using below code
var userId = FB.Facebook.apiClient.get_session().uid;
if(userId > 0 || userId != '')
{
alert('user is already log in');
}
hope it's helpful to you

Posting in wall with not logged user (using offline token)

I need to post on the wall of a user as if it were itself (offline token), but when logged in with another user. Example:
I login with user X. In some moment I want to post on the wall of the user Y as if he had posted (using offline token).
Ps:
I do not want this:
var wallPost = {
message : " My message",
};
FB.api('/USER_ID/feed', 'post', wallPost , function(response) {
if (!response || response.error) {
console.log("facebook error: " + response.error);
}
});
I need the post is in "FB.api ('/ME/feed', 'post', wallPost, function (response) {"
But this "ME" is not the logged user, but the user owns of the offline token.
You cannot do that with the Javascript API because the FB.api call will only use the access token for the currently logged in user. You will need to build a call directly to HTTP post the message to the https://graph.facebook.com/me/feed?access_token={the offline access token you have}
SOLUTION:
Just insert the acess_token in JSON parameter.
var wallPost = {
message : " My message",
access_token : "ACCESS_TOKEN_OFF_LINE_OF_THE_USER"
};
FB.api('/me/feed', 'post', wallPost , function(response) {
if (!response || response.error) {
console.log("facebook error: " + response.error);
}
});

Post to User Wall using offline_access in facebook Javascript Sdk

My problem is that i want to post to user wall using the offline_access.
I donot know how to do that using the facebook javascript sdk
with this method FB.api('/me/feed')
Please also give me an example how to integrate the facebook connect with asp
If the user has already authenticated and has given publish_stream or offline_access permissions, you can just call this method to publish to their feed:
var body = 'Reading Connect JS documentation';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});