How to programmatically post like and comment an URL on facebook? - facebook-graph-api

This is confusing. So just to clarify:
REQ #1: To fetch basic stats for a URL, you send GET request to:
http://graph.facebook.com/?ids=http://some.com
(alternatively, FQL can be used to fetch stats for a URL, but that doesn't return the OpenGraph Object)
REQ #2: To fetch comments for a URL, you do a GET:
http://graph.facebook.com/comments/?ids=http://some.com
REQ #3: To fetch likes for a URL, you GET:
http://graph.facebook.com/likes/?ids=http://some.com
But how do you comment on / like a URL programmatically?
I guess, likes can be created using Open Graph API, right?
https://graph.facebook.com/me/og.likes?object=1234
where 1234 is the OpenGraph Object ID of an article (as returned by REQ #1).
But this requires an approval process, the like action has to be approved by Facebook.
Is there a better way to do this? Can I use for example the Graph API for all these things?
My goal:
Currently I'm using the Facebook like button and comments plugin to create likes and comments. But these use the JS SDK, which is huge, and they generate a lot of external requests. I wanna get rid of them and just send an AJAX request to my server, which would then asynchronously talk to Facebook, and it would post the Like / Comment.
Is this possible?
Thanks in advance!

I read through the Facebook docs briefly and I don't believe you can do this other than the way you indicated with authenticating.
You should also take a look at this thread: 'Like' a page using Facebook Graph API

You would need to authorize every single user before he would be able to like something, and you would need to go through a review process on Facebook. And i am pretty sure you would not get the required permissions approved just because you want to get rid of the JavaScript SDK overhead, to be honest.
The Social Plugins are asynchronously, so the overhead for downloading the SDK is irrelevant as it happens in the background and it is non-blocking.

I have an idea, to do this. You can use long term access token, Once you login you receive short term token. After receiving short term token you need to request your long term access token. Save that token in DB or file.
Then you can use Graph Api, to make requests. This will eliminate the need for access requirement every time you request api.
Just use access token you saved before.
Refer this documentation from Facebook for further clarity.
https://developers.facebook.com/docs/facebook-login/access-tokens
Happy Coding!
Atul Jindal

Related

facebook graph api pages post message

I wanted to make a facebook API request that will post a message to page that I am admin of. Here i found out how can i do that but after trying it out in facebook graph api explorer with this scheme:
graph.facebook.com/page-id/feed
?message=Hello&access_token=your-access-token"
assuming I have a developer account linked to my page and publish_pages and manage_pages permissions are enabled and page-id and acces-token are replaced with my real ones. The problem is that I get a response of latest post on that website, the same as if I would write just graph.facebook.com/page-id/feed and there is no new post on my page.
I dont know if the recent facebook API update removed or modified that or if its just not possible.
trying it out in facebook graph api explorer with this scheme
graph.facebook.com/page-id/feed?message=Hello&access_token=your-access-token
You are making a GET request here - and GET is for reading data, not creating it.
Creating data requires a POST request. So you can either switch the request method from GET to POST via the dropdown there, and then click + Add parameter to add your parameters and values;
or you can add &method=post to the end of your GET request query string here - that is a way the API offers to explicitly overwrite the request method in environments, where you can only make GET requests.

Request a Page Access Token in C# SDK

As a proof of concept for a simple background application, I used the Graph API Explorer to create an access token for my app to post something to the wall of a page I maintain. It worked fine. Naturally, however, the token expires.
So now I'm trying to have the background application automatically request a new page access token each time it runs. And I'm having a lot of trouble finding a concrete definition of how to do that. There's no shortage of information regarding Facebook and Access Tokens, but nothing seems to demonstrate how to have a background application post to a page. (Not post to a user's wall, not display a login dialog to a user since it's a background application, etc.)
I can fetch an access token in code easily enough by reading the response from a web request to this URL:
https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={MY_APP_ID}&client_secret={MY_APP_SECRET}
Of course, that "access token" doesn't work when trying to post to the page's wall. It says that the user hasn't authorized the application to perform this action. The action I'm performing is pretty simple:
var client = new FacebookClient(GetFacebookAccessToken());
dynamic parameters = new ExpandoObject();
parameters.message = "this is a test";
dynamic result = client.Post("{MY_PAGE_ID}/feed", parameters);
I've read in some places that I'll need to make a second request, using the first access token, to get the page access token. But I can't seem to find examples of how to do that.
Can someone shed some light on this for me?
I have a Facebook page.
I have a Facebook app which serves no other purpose than to provide a means for a local background application to access said page.
I just need that application to be able to authenticate so it can post something to the page.
(And if there's a step I need to perform in the Facebook UI to permanently give the application permission to do this, I think I've performed that step but it would be good to double check somehow.)
Edit: It's been described to me that I need to obtain a long-lived user access token and, using that, obtain a page access token. The theory is that said page access token won't expire. However, what's not clear to me is how one accomplishes this.
I've read the page describing the deprecation of offline_access, as well as the page describing server-side access. However, I'm clearly misunderstanding something. In the former, it references the latter for obtaining the proper token. The latter, however, includes steps for presenting a login to the user, having them accept permissions, and using the response from that login.
Being a background process that runs unattended, presenting any sort of question to a user (which would be me) isn't really an option. I've also been told that I can't do a one-time request from my browser to get an access token because that is, by definition, client-side interaction and not part of the necessary server-side flow. (It seems odd to me that the service would care if a RESTful request comes from a web browser vs. from an application, but I'm not familiar enough with OAuth or the Facebook API to really make that call.)
So, if I can perform some manual steps to get a permanent access token for the app to post to the Facebook page, what are those steps? Conversely, if I can perform some automated steps in the application to acquire access each time it runs, what are those steps?
(Making a few more API calls from the application adds a second or two of running time to an otherwise once-a-day process, so it makes no difference to me which approach to take.)
At first I just went into the Facebook Application settings and re-enabled the deprecated "offline access" permission. Said application settings can be found at a URL like this:
https://developers.facebook.com/apps/{APPLICATION_ID}/advanced
However, since everything keeps referring to that setting as being "deprecated" then I didn't want to use that as a long-term solution. It may get removed entirely, it may be unsafe in certain circumstances, etc. Better to use recommended functionality.
So here's what I was able to piece together from a scavenger hunt through updated documentation, outdated documentation, a sea of outdated internet posts, and PHP code which mostly made assumptions about functionality that aren't true in all cases...
Visit the Graph API Explorer and select your Facebook Application from the drop-down menu. Click "Get Access Token" and select the permissions you want. (For mine I went to the "Extended Permissions" tab and selected "Managed Pages" and "Publish Stream.") You will be prompted (in my browser it was in a new tab) with a familiar screen where the Facebook Application is asking you, the user, to grant it the permissions you just selected. (You've seen this before if you've ever agreed to use a Facebook Application before.)
The value it produces in the Graph API Explorer (a long string of random-ish characters) is your "Short Lived User Access Token."
As described here in "Scenario 4: Client-side OAuth and Extending Access_Token Expiration Time through New Endpoint" access this URL in your web browser:
https://graph.facebook.com/oauth/access_token?
client_id={APPLICATION_ID}
&client_secret={APPLICATION_SECRET}
&grant_type=fb_exchange_token
&fb_exchange_token={SHORT_LIVED_USER_ACCESS_TOKEN}
(You can obtain the {APPLICATION_SECRET} value on the basic settings page for your Facebook Application: https://developers.facebook.com/apps/{APPLICATION_ID}/summary)
This will return another Access Token as such:
access_token={LONG_LIVED_USER_ACCESS_TOKEN}&expires=5184000
This access_token value (another long string of random-ish characters) is your "Long Lived User Access Token." The expires value is in seconds, which translates into 60 days.
Now we hop over to the Page API reference and take a look at the section on Page Access Tokens. This, along with the basic structure of Graph API requests exemplified here (scroll down to the part where it shows a bulleted list of sample links which include access_token specifiers, which you'll need to specify here because you're requesting non-public information) leads you to request this in your browser:
https://graph.facebook.com/{FACEBOOK_USER_ID}/accounts?
access_token={LONG_LIVED_USER_ACCESS_TOKEN}
This will return a JavaScript object containing lots of useful information about the Facebook Pages and Facebook Applications your user account controls. In my case the Page and the Application had the same name, but it's easy to tell them apart from the category values or, if all else fails, the id values. Find the Page that the background application running on your machine will need to access and copy its access_token (the third and final long string of random-ish characters). The whole node looks something like this:
{
"name": "Some Facebook Application Name",
"access_token": "{LONG_LIVED_PAGE_ACCESS_TOKEN}",
"category": "Musician/band",
"id": "{APPLICATION_ID}",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
}
This is your "Long Lived Page Access Token." This is the value you use to initialize the FacebookClient object in the code. Then, posting a simple status update is as easy as:
var client = new FacebookClient("{LONG_LIVED_PAGE_ACCESS_TOKEN}");
dynamic parameters = new ExpandoObject();
parameters.message = "This is a my status update.";
dynamic result = client.Post("{FACEBOOK_PAGE_ID}/feed", parameters);
Supposedly this "Long Lived Page Access Token" does not expire after 60 days like the "Long Lived User Access Token" does. I'll find out in 59 days, I guess.
NB: The curly braces in my examples are part of the placeholder for actual values. Do not use the curly braces in the actual requests. So something like this:
https://developers.facebook.com/apps/{APPLICATION_ID}/advanced
becomes something like this, for example:
https://developers.facebook.com/apps/123456/advanced
where 123456 is the actual Facebook Application ID.
Being a background process that runs unattended, presenting any sort of question to a user (which would be me) isn't really an option.
As I already said, you only have to do it once.
You get your non-expiring page access token, copy&paste that into your app – and from then on your app can do server-side whatever it wants to do happily everafter.
I've also been told that I can't do a one-time request from my browser to get an access token because that is, by definition, client-side interaction and not part of the necessary server-side flow.
The server-side auth flow for getting a user access token also needs to take part partly in the browser.
It does not matter, if you get a short-lived token via the client-side auth flow and extend it afterwards, or if you get a long-lived one using the server-side auth flow.
(It seems odd to me that the service would care if a RESTful request comes from a web browser vs. from an application […])
Facebook does not want users to give their login credentials to any third party. Therefor, the process of getting a user access token always has to take part in the browser, with the user login in to Facebook.
So, if I can perform some manual steps to get a permanent access token for the app to post to the Facebook page, what are those steps?
Get a long-lived user access token with manage_pages permission. (Or get a short-lived one, and extend it). And then, use that long-lived token to request a page access token for the target page, in the way that is described in the docs.

Offline_access and Api-Key confusion

I've tried do develop automatic code that would fetch data from ads campaigns under account. My plan is to have server that would query every day to get data from impressions/clicks/costs of ads and save it for later use.
I use https://developers.facebook.com/tools/explorer to get access_token for getting data (insights) from those campaign pages but when I log out to try offline_access it tells access_token is old, cause of password change or access_token getting outdated.
Isn't the whole point of offline_access to grant acccess when user is not logged in?
And besides that.. what's with most of my calls ending having reply as follows:
"(#294) Managing advertisements requires the extended permission ads_management, and a participating API key"
I've given access_token that has ads_management, but I have no idea what this Participating Api key even means. Do I need to add this to url call or something? Where do I get this Api Key?
Edit: I want to be able to get data from add campaign pages, so I'm not talking about Apps in page
You will want to read up on the deprecation of the offline_access here: https://developers.facebook.com/docs/offline-access-deprecation/
But most importantly follow the guide here on how to handle invalid access tokens:
https://developers.facebook.com/blog/post/500/
You app is to monitor the error messages coming back from the API and handle it gracefully.
I have the same issue and I found this bug can be reason of our problem. It's already accepted and assigned, but still not solved. I hope it's really bug and not a secure hole, that was closed :)

Fetching one's old Facebook status messages

I want to fetch one's old Facebook status. I have written the code for the same but it looks like it is not working for me. The steps that I have followed are as follows:
First I use the below API to seek the code from Facebook (by prompting user to log in to the Facebook page)
https://www.facebook.com/dialog/oauth?client_id=xxyy&redirect_uri=http://xxyy/home.aspx&scope=publish_stream,read_stream";
Then I seek the AccessToken by passing the code obtained in the above point to the below api as:
https://graph.facebook.com/oauth/access_token?client_id=xxy&redirect_uri=http://xxyy/home.aspx&client_secret=xxyy&code=zzz"
Now there is a problem. I am not able to understand which API I need to use to fetch the old status of the logged in user. I guess I have the access token which is the key to fetching the info. But I am not able to find the relevant API.
I would recommend that you use FQL.
//PHP example:
$fql_query_url = 'https://graph.facebook.com/'
. '/fql?q=fql?q=SELECT status_id, message FROM status WHERE uid=me()'
. '&' . $access_token;
$fql_query_result = file_get_contents($fql_query_url);
The graph API and "post" feed is what you need. I don't usually do this but I need my old statuses!
Following code works for me for fetching Facebook Conversation:
String s = facebook.request("me/inbox");
System.out.println("Message...."+s);

How to Follow Twitter Users by API in Coldfusion?

Oauth is pretty complicated but I think I got the authentication working. I got up to the point where I get the user to authorize on Twitter and I have the access token and access token secret. From here, I'm not sure how I use it to generate a request to follow a user.
I know that the url is: http://api.twitter.com/1/friendships/create.xml and that you can either pass in a user_id or screen_name
but does anyone have any example code in Coldfusion that I can see as to how the cfhttp call looks like with all the parameters?
Thanks in advance.
Try (monkeh)Tweet Twitter API. It supports most of the twitter API methods including follow a user. Easy & freaking cool CF open source by Matt Gifford