Facebook API without client authentication for public content - php

I'm developing a site for a client who already have the photos of his products on Facebook, and he wants the same albums to be replicated over his site. I was already using Facebook Connect, so I dropped a combination of photos.getAlbums and photos.get to dynamically make the galleries.
So far so good, but then I realized that if there's no user logged trough FBC, no session is created and the API becomes unusable, even for content that is publicly available. I obviously want to show the albums to everyone and not just the people who connect their accounts.
Is this how all functions in the Facebook API work? What's the best (easier to implement) workaround for this situation?

As of September 2010, you can now authenticate just as an application, with no associated user. See http://developers.facebook.com/docs/authentication/ for details. Or you can run this example code to get an access token:
curl -F grant_type=client_credentials \
-F client_id=your_app_id \
-F client_secret=your_app_secret \
https://graph.facebook.com/oauth/access_token

For the record, I managed to solve this situation by developing a small backend that requires the client to login to Facebook once and give offline_access extended permission to the FB app, so I can save his session key and use it to authenticate the Facebook API client every time I need to use FQL to get non-public content.
One could obviously add some caching in the middle to avoid unnecessary requests to Facebook, but it's been working fine for months now without hitting any limits that I know of.

That doesn't make sense to me - I have a (relatively simple) app that renders in facebook even if the user has never logged into facebook before (in which case it displays demo data).
When using the facebook PHP library, I just do this:
$facebook = new Facebook($api_key, $secret);
No session id required - but, obviously, api functions that depend on information about the user aren't going to work.
You can also look into an "infinite session" for your app - you could create an infinite session key for yourself and use that session to access the API.

Related

Add users post to their facebook newsfeed automtically with php

I have a website I'm working on. It enables users to register and post stuffs within the site. Can I get a link to some sort of api that allows people to add their facebook account (once) and then automatically post what they posted on my site on their facebook newsfeed (the sort of thing that happens when you connect your facebook account to your twitter account) .I've searched and can't seem to get exactly what I want
What you want is pretty common and if you searched you clearly didn't do a good job on the search.
Using the Facebook PHP SDK is pretty straight forward. There is a PHP Library available with a demo of the functionality you want. In your case you need to do some stuff more, so I'll explain globally what you should do:
Get the Facebook PHP SDK and load it into your website, determine what scope you need to perform the actions what you are going to do, in your case you need to have access to their timeline which is called the publish_stream scope. In order to get the Facebook PHP SDK working you need to create an app at http://developers.facebook.com
If a user grants access you need to save the authentication token that the user provides and save it in your database for later use. I'm not 100% sure how long they are valid, I think it's maximum of 30 days at the moment, but you have the ability to refresh the toking in the requests you make (correct me if I'm wrong, it's been a while..)
Everytime an action is done using the Facebook API you inject the usertoken in the request, thus performing the "post" action onto their timeline.
What you are looking for is pretty straight forward and easy to find..

How can I show my personal stuff from Facebook in my website?

I searched in Stack Overflow, Google, etc, but I can't figure out how to show my stuff from my Facebook on my website. I tried some solutions and each one prompted the login dialog and visitors had to log in and accept my application (I've created one to test).
Many answers here were too generic, I didn't understand them that is why I'm asking.
I want something that would let my website "log in" to Facebook and show my status or photos to visitors without them needing to accept an app or log in into Facebook with their account.
I tried a facebook-php-sdk example with my AppID and AppSecret and it asked me to log in.
Also, github.com/facebook doesn't have an SDK for Python similar to facebook-php-sdk
You could write a script (eg. using the FB PHP SDK) that uses a long-lived access token to fetch your FB data and then store the data in your backend database (or other store for your website). To Facebook, your script will look like an app and your machine will be the only 'user' of that app. Note, long-lived access tokens are good for 60 days max. You could also try using an App Access Token to fetch basic info. App Access Tokens don't expire.
This might not be exactly what you are looking for since you posted this with the php and python tag, but it might solve your problem.
If you're website is powered by the wordpress engine you can use If-this-then-that : https://ifttt.com/
It basically allows you to create "recipes" with something like:
if new status on Facebook then create post on wordpress
The post will not require others to log in to Facebook.
This is without writing a single line of code only a couple of mouse clicks. So I'm not sure if this satisfies your needs.

Easy way to get facebook statuses from a particular page in PHP?

I need to grab the statuses from a Facebook page via PHP to display on the page. I don't want to have people logging in to facebook just for my script to grab the statuses from the page since it is a public page and isn't restricted. I have the Facebook API for PHP in my projects directory but have no idea how to use it to do what I want it to. Could someone give me a code example of how I'd do this? Thanks!
It's really a straight forward task if you follow the documentation. Since /PAGE_ID/feed requires any valid access_token, your app access_token is enough to get the data:
Open the Graph API Explorer (I'm querying coca-cola/feed)
Get an app access_token from the Access Token Tool and use it instead of yours
You're done!

Pulling Public Facebook Posts Into PHP Page

Last December, I set up an app for a client who wanted to pull their public facebook posts into the footer of their website.
I was using this URL to pull the message, create time and permalink and when I wrote it, all was good:
http://api.facebook.com/method/fql.query?query=SELECT%20message,%20created_time,%20permalink%20FROM%20stream%20WHERE%20source_id=138631878804%20AND%20message%3C%3E%27%27%20limit%203
Today they reported that it was no longer pulling their information. I put that url into a browser and the result is "Requires valid signature"
I've gathered from google that I need something called an access_token, also I've seen where I can use the php sdk but then I need an appId and a secret token.
At this point, I've read so many different sources that I am thoroughly confused. The FQL query above represents the full extent of my Facebook programming experience. It looks like maybe I want the Graph API, but I don't want anyone to have to sign into anything.
Can one of you guys who are more versed in the voodoo of facebook nudge me in the right direction to do this:
From PHP, pull public messages from the stream table without forcing a user to log into facebook.
Thanks!!
To pull posts from a publicly visible Page on Facebook, you can use any valid access token. For most sites managing a page, this will be a page access token retrieved via one of the page's admins authorising your app to have access to their pages, but that might be overkill for your use-case.
Probably the quickest solution is to just create a new App ID for the site, get the app access token for that App (see 'App Login' on https://developers.facebook.com/docs/authentication/ ) and then use that access token to retrieve the posts.
I found several links all from a simple Google search.
Duplicate question: https://stackoverflow.com/questions/3298836/how-you-get-access-tokens-for-facebook
http://benbiddington.wordpress.com/2010/04/23/facebook-graph-api-getting-access-tokens/
https://developers.facebook.com/docs/authentication/

What is the simplest way to add Facebook Connect to a PHP web site?

Facebook Connect
I have spent a few weeks putting together a basic web site which uses Facebook connect as authentication.
I have studied 'theRunAround', the provided example application. However the code is convoluted and uses a large number of files and classes, most of which have a lot of functionality that I have no need for. I have also had a good read of the documentation.
My code works almost all the time. However, occasionally I receive strange, unpredictable errors.
Required Features
Check if user is already logged in to Facebook, if so, retrieve the fb_uid, name, profile pic
Return a list of friends which have also connected to this application
Are there any tutorials, better than the ones provided by Facebook regarding such simple functionality?
I wrote my own library to work with Facebook Connect as I found the one provided to be very lacking.
I can't provide the code as it is company code, but here's a breakdown of what I needed to do. Hopefully you can use it and fill in the blanks easily yourself.
In your application settings, configure the "Connect" tab. The "Connect URL" in my case is the root folder where my xd_receiver.htm file is. Also, under "Advanced" I marked my app as a Web Application.
Put your xd_receiver.htm file in the root folder specified above. There is a ton of documentation on xd_receiver.htm out there. This is what facebook hits when the user logs in. It will write cookies to their browser which your application can read in later to do authentication.
The cookies Facebook sets are in the format of _ where APIKEY is your apps API key and is the name of the cookie. You'll need the _session_key cookie to make further API calls. If this cookie is not set, you need to show the login button as desscribed in steps 4 - 6. Otherwise skip to step 7.
You need to load in the Facebook javascript file on your page that you will have the FB login button. ==> http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php
To show the FB login button, use:
The Facebook JS will automatically render a facebook login button for you. It will trigger the "facebook_onlogin" method that you shall define once the user is logged in so you can do something after they login.
Right below the above markup, you need to call the FB init javascript to have it render the button:
FB.init('YOUR API KEY HERE', 'ABSOLUTE PATH TO YOUR XD_RECEIVER.HTM FILE HERE');
Use the session_key as set in the cookie to make any API calls. How to make API calls is well documented.
Hope this helps.
I have recently added a friend notification feature to Cogenuity using the facebook connect technology.
First, you need to create the facebook object in your php code. I am assuming that you have already done the facebook application registration goodness. I found a lot of the parts to facebook connect not to work but what did work was this.
$user = $facebook->require_login();
I found that the FQL parts worked pretty good.
$query = "select uid1 from friend where uid2 = {$user}";
$results = $facebook->api_client->fql_query($query);
I used this FQL query which may serve your needs.
$query = "SELECT name, pic_square, status, about_me FROM user WHERE uid = {$uid}";
$results = $facebook->api_client->fql_query($query);
As does the notification_send method.
$facebook->api_client->notifications_send($notifyTarget, $intro, 'user_to_user');
I hope this helps.

Categories