Is it possible to publish action from a Facebook Page? - facebook

I've been using the great facebook OpenGraph and the publish_actions permission. My users can publish actions through our website, and it's directly connected to there Facebook-Timeline. Great integration, very simple to be developped : thanks a lot for your work!
But, I have one more question :
Is it possible to use publish_actions not from a specific user but directly from a Facebook Page? (as described here for a user: https://developers.facebook.com/docs/opengraph/actions/#create)
For example, User John Smith has one Facebook Page (named "John Smith Advocate"), and wants to publish action, not from John Smith himself, but from the Facebook Page "John Smith Advocate".
I wonder if it's possible.
Thanks a lot guys !

In order to publish as a page, you need to obtain an authentication token for that page. To do this you need to follow these steps (sorry for the lack of detail, hopefully this will point you in the right direction):
1) A user who is an administrator of your page needs to be authenticated with your app, and you need to get the "manage_pages" extended permission for them
2) get a list of all the accounts the user is an admin of:
https://graph.facebook.com/me/accounts
3) you'll get a json array of pages the user is an admin of, and also an access token for each page - extract the relevant access token
4) Use the access token to post to the relevant album ID (owned by the page) - here is some php code that does it;
<?php
$args = array('message' => 'Caption');
$args['image'] = '/path/to/image.jpg';
$args['access_token'] = $page_access_token;
$data = $facebook->api('/'. $album_id . '/photos', 'post', $args);

Related

Combining selenium with facebook api and making request

I have 3 questions: 1)Can I combine facebook login via mozila browser(selenium) with facebook graph api, for example:
browser=webdriver.Firefox()
browser.get('https://facebook.com')
user=browser.find_element_by_id('email')
user.send_keys('email')
password=browser.find_element_by_id('pass')
password.send_keys('pass')
login=browser.find_element_by_id('u_0_r')
login.click()
graph = facebook.GraphAPI(token)
profile = graph.get_object("me")
friends = graph.get_connections("me", "friends")
friend_list = [friend['name'] for friend in friends['data']]
print (friend_list)
2)Can somebody help me with login on facebook through app_id, secret_id and token, or through email and password
3)I want to get profile info, for example with help of get_object, but I need to know user's id to do it. How can I get this id, if I have only page url's, or I can use part after facebook.com/ to do this request?
P.s. If u know answer at least on one of this questions, pls right it in comments or in answers, it will be very useful:)

Facebook access token for post message in timeline

I am trying to do something like this. Lets say a user used FB Connect for registration in our site, so I can get the "uid" of facebook and I can store in DB. Now what I want each time that user will visit a store details page or item details page I will post that store/item image with link, photo, description etc to FB timeline.
Something like this:
$post_id = $facebook->api('/me/feed/', 'post', array(
'message' => $products_name, // item name
'link' => 'http://www.blabla.com/item/myshoes', // item url
'picture' => $fb_img_src, // item image
'caption' => $products_name, // item name for caption
'description' => $products_description // item description
));
} catch (FacebookApiException $e) {
$user = null;
}
This process works fine if user logged in to FB by using our FB app. But as I said I want to post if they not even logged in by using their facebook "uid".
Is it possible to authenticate that user depending on facebook "uid"?
Thanks in advance! Any clue/help will be appreciated!
I strongly believe that posting anything to user's Facebook without their's consent or even their action will surely and quickly encourage them to leave your site forever. Do not do this.
Anyway - to answer your question:
Yes, it is possible to get access to user's Facebook and "do stuff", especially when a user is logged in both on Facebook and on your site. You just need to obtain user's access token (read about it in the docs), and make sure the users grants your app all aproppriate permissions. There are also access tokens that can be used offline (user is not even online), but I'm not going to discuss it here.
One way to obtains user's access token is to redirect the user to FB login url providing your APP_ID (as described in FB developers docs). FB will then redirect the user to your Fb-login URL with access code/access token, which enables you to do something like posting to users timeline.
Of course actions that you can take are limited according to permissions said user has granted for your app. And I have to remind you - it's a thin ice you're stepping on.

testing facebook real time updates / user comments

I'm trying to integrate realtime updates for user pages. I have a callback running on a given HOST url. Having this the subscription creation is:
G = facebook.GraphAPI()
app_token = config['FACEBOOK_APP_ID'] + '|' + config['FACEBOOK_SECRET']
path = config['FACEBOOK_APP_ID'] + '/subscriptions'
post_args = {'access_token' : app_token, 'callback_url' : HOST, 'fields' : 'feed', 'object' : 'page', 'verify_token' : 'token'}
G.request(path, post_args=post_args)
This seems to work just fine and a subscription is created. After this, for a given user I get oauth credentials with perms = ["publish_stream", "offline_access", "manage_pages", "publish_actions", "read_stream"]
Using this access token, I do a subscribe by adding the app to the page tabs:
G = facebook.GraphAPI(access_token)
path = "/%s/tabs" % page['id']
G.request(path, post_args={'app_id' : config['FACEBOOK_APP_ID']}
Now things work.... sort of. First thing is I'm not sure exactly how facebook testing should work in practice. Basically I created a bunch of test users using the app settings on developers page. Then using one of these users, I create two separate pages Page1, Page2 and I registered the app as a tab on these pages. Posts issued from the test users, or from the real user I created the app with are picked up. But I try to find one of the pages from some other real user (so not a test one and not the one I created the app with) and I cant. Even if I copy paste the entire URL, I just get a redirect to the first page. Are those pages only visible inside my own test app/users context ? Should I create a page from my real account and test with that? I'm just curious on how would I go around testing this in a real setup. Would I get ALL comments / posts on that page regardless of the user who does the posting or just posts from the Page / Admins of the page ?
Another separate problem I'm having is creating a comment from some user. So I'm using the exact same access token I've got above, and having a post from someone come it, I want to issue a reply in the form of a comment on that post. I have the facebook post id, so I just:
graph = facebook.GraphAPI(access_token)
graph.put_comment(facebook_post_id, message)
Say I issue this command using the access token I got from TestUser1 on a post that came on his own page Page1 . I would expect that the comment be posted on behalf of TestUser1 but instead it gets posted on behalf of Page1 . Is this expected as a side effect of the token I'm using? I've read here https://developers.facebook.com/docs/facebook-login/access-tokens/ that there are user access tokens and page access tokens; but I've tried the exact same thing with a token without the 'manage_pages' perms and still get the same thing.
Cheers, Bogdan

How to post to a Facebook Page (how to get page access token + user access token)

I am trying to work out how to post to a Facebook page wall, when using my app as a different Facebook User (who is not the Page Administrator).
I get a range of error messages while testing:
Exception: 200: The user hasn't authorized the application to perform this action
The page administrator has visited the app and accepted the following permissions: publish_stream, manage_pages, offline_access
Here is the code I plan to use:
// Insert Page Administrators ID here
// This user is not the same user that is currently logged in and using the app
// This user is the page administrator who has authorised:
// - manage_pages
// - offline_access
// - publish_stream
$user_id = '123456789';
// Insert Page ID here
$page_id = '123456789';
$accounts = $facebook->api('/'.$user_id.'/accounts');
foreach($accounts['data'] as $account)
{
if($account['id'] == $page_id)
{
$page_access_token = $account['access_token'];
echo "<p>Page Access Token: $page_access_token</p>";
}
}
// publish to the wall on your page
try
{
$result = $facebook->api(array( "uid" => $page_id,
"method" => "stream.publish",
"access_token" => $page_access_token,
"message" => $message, ));
}
catch (FacebookApiException $e)
{
error_log('FB Error: Could not post on Page Wall. Page ID: ' . $page_id);
error_log('FB Error Message: ' . $e);
}
Note: There may be PHP errors in the code above, as I just spliced it on the fly, but its not so much the PHP errors I need correcting, but more my logically understanding of how I am meant to go about this process.
PROBLEM:
I can't access the $user_id/accounts information without an active user access token for the Page Administrator.
The end result that I'm trying to achieve is:
1.) A normal FB user goes to the app and submits a form
2.) The app posts a message on a FB Page wall, which is not owned by the FB user, but has previously been authorized by the Page Administrator with the following permissions manage_pages, publish_stream and offline_access
Q1. Since the Page Administrator has accepted the appropriate permissions, why can't I just generate an active user access token, without the actual Page Administrator user logging into the website?
Q2. Is there a way I can get the equivalent of /$user_id/accounts for the Page Administrator user_id, when logged into Facebook as a different user (which is why I do not use /me/accounts)?
Q3. Please confirm that my understanding of needing the page access token to post to the page wall is correct (or do I need the user access_token for the Page Administrator - see Q1)?
Q4. Anyone have a handy resource on what each type of access_token can actually access?
If you need any more information, please let me know.
I've spent the last few days working on this and I'm stuck.
Thanks!
You can ask the page admin for manage_pages along with offline_access. I do this in my production app to be able to post scheduled postings onto the pages' walls.
Nope. Not possible. That's what asking permissions is all about. And why not everyone gets to administer everyone else's pages. Could you image if you could administer anyone's page without them granting you access?!?
To post to the page as the page, you need a page access token. To post to page's wall as a user, you need a user access token.
Yes, please see: https://developers.facebook.com/docs/reference/api/permissions and https://developers.facebook.com/docs/authentication/
If you have further questions about any one of these, please start a new question. It's not really fair to users of stackoverflow to be hit with 4 questions in one and then to be asked followup questions to each of those.
I have done in Django:
Step to get Page_access_token:
facebook_page_id=360729583957969
graph = GraphAPI(request.facebook.user.oauth_token.token)
page_access_token=graph.get(facebook_page_id+'?fields=access_token')
This way you can get Page access token.
You can check this thing on Fb GraphAPIexplorer:
http://developers.facebook.com/tools/explorer
GET URL: fb_page_id?fields=access_token
for example: 360729583957969?fields=access_token
that will give you page_access_token

Send a personalized URL to Facebook friends from external website

I'm developing a website with the following requirement:
Users may invite their facebook friends to visit the website
Invited friends receive a personalized URL that points back to the website (either a message, notification, request...doesn't matter)
Example flow:
1. John visits http://www.mysite.com
2. John clicks the Facebook connect button and logs in with his facebook credentials
3. John clicks 'invite friends' button. The friend selector dialog appears
4. John selects Mary and submits the invitation/request/message
5. Mary receives a link like: http://www.mysite.com?name=Mary
6. Mary clicks the link from within facebook and is redirected to the URL above
7. My site displays: "Hello Mary!", taking the name from the URL parameter
I already managed to make steps 1-4.
I'm asking about step 5. Is that possible at all? I know I can get the facebook IDs of the people selected in the multi-friend selector, and that I can make a FBQL query to retrieve the names of the people selected, but how to customize the URL sent?
Cheers,
Nacho
Well, if the user will be sending invitations for a bulk of friends then these invitations won't be "invitee unique" (As mentioned by #Jimmy). BUT you can still capture the unique "friend id" and (I suppose) name from the request itself!
What you need to do is the following:
When a friend clicks on the invitation, he/she will get redirected to the canvas page (or the bookmark page if it has been sit).
Along with the redirection you'll receive the invitation number (request id) here you can read this request and extract the "To" field from it and then redirect the friend to your website.
Something like:
<?php
if( isset($_REQUEST['request_ids']) ) {
// Requesting an application token
$APPLICATION_ID = "APP_D";
$APPLICATION_SECRET = "APP_SECRET";
$token_url = "https://graph.facebook.com/oauth/access_token?" .
"client_id=" . $APPLICATION_ID .
"&client_secret=" . $APPLICATION_SECRET .
"&grant_type=client_credentials";
$app_token = file_get_contents($token_url);
// You may have more than one request, so it's better to loop
$requests = explode(',',$_REQUEST['request_ids']);
foreach($requests as $request_id) {
// Get the request details using Graph API
$request_content = json_decode(file_get_contents("https://graph.facebook.com/$request_id?$app_token"), TRUE);
// An example of how to get info from the previous call
$to_name = $request_content['to']['name'];
// When all is done, delete the requests because Facebook will not do it for you!
$deleted = file_get_contents("https://graph.facebook.com/$request_id?$app_token&method=delete"); // Should return true on success
echo "<script>top.location.href='http://www.mysite.com?name=" . urlencode($to_name) . "'</script>";
}
}
?>
For more read the following:
Facebook Documentation
How To: Handle Application Requests Using The Facebook Graph API
When the referred user lands on your site, you either need to know their Facebook User ID or all the information you plan to show them. It's going to be easier and more extensible to use the User ID, and to get that you'll need to get that user to install your app before you can identify them, look up their name, and say "Hello Mary!"
What I'd do here is let the referring user share a link customized for him. So if I'm A, and I'm sharing with my friends, my link would look like http://www.somedomain.com/?ref=A. I share this, my friend B clicks the link. When B gets to the install screen, he installs, and now we know B's name, and since we have the ref parameter, we know he clicked my link. (This is how Groupon and Dropbox handle referral links.)
And to answer your question more clearly, no, invite links need to be the same for each invitation you send using the multi-friend selector, so per-invitee links are not an option.