A Working Example of RestFB

7,060 views
Skip to first unread message

kintu

unread,
Feb 11, 2011, 4:15:04 AM2/11/11
to RestFB
Hello, I an trying to configure my facebook connect to accept any auth
token for any user.

when i try to use DefaultFacebookClient() it want to me to enter auth
token.

how do i configure so that it would get auth token automatically for
any user.


can you post a simple working example that gets user's email, name and
friend list? so when i run it i can login in using any of my 3
facebook accounts and retrieve right information.

thanks


I cant find a working example or sample of this RestFB anywhere else.
not even on project home page. it goes on about other things but not
setting up authorisation tokens.

revetkn

unread,
Feb 11, 2011, 7:37:32 AM2/11/11
to RestFB
The RestFB docs go on about other things because they assume you are
familiar with the basics of the Facebook API.
This is a good start for learning about how authentication works:
http://developers.facebook.com/docs/authentication

Thanks
Mark

kintu

unread,
Feb 11, 2011, 11:46:16 AM2/11/11
to RestFB
but how do i implement web browser on java, java doesnt have web
support like c#

kintu

unread,
Feb 11, 2011, 11:59:45 AM2/11/11
to RestFB
I got this code can you help me?
can you fix this for me so i get user's name and email

thanks


package facebooktest1;

import com.restfb.*;
import com.restfb.exception.*;
import com.restfb.json.*;
import com.restfb.types.*;
import com.restfb.util.*;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Main {

String API_KEY = "d33e82abxxxxxxxxxxxxxxxxxxxxxxx";
String SECRET = "62dd6c73xxxxxxxxxxxxxxxxxxxxxxx";
static FacebookClient facebookClient;
String facebookAuthToken = null;
String facebookSessionKey = null;

public static void main(String[] args) {




facebookClient = new DefaultFacebookClient("2227470xxx|
2.PnVz9uj4blyh4iR_bGss4Q__.3600.1297386000-561331950|bNfq-
xK8_Dyt2Ck1SY-g2xxxxxx");




// I always call facebookAuthorise
// First time users would click a button that calls
facebookConnect() first to grab the session key.

facebookAuthorise();
}

private void facebookConnect() throws FacebookException {
// auth.createToken returns a string
// http://wiki.developers.facebook.com/index.php/Auth.createToken
facebookAuthToken = facebookClient.execute("auth.createToken",
String.class);
//facebookAuthToken =
facebookClient.executeQuery("auth.createToken", String.class);
String url = "http://www.facebook.com/login.php"
+ "?api_key=" + API_KEY
+ "&fbconnect=true" + "&v=1.0"
+ "&connect_display=page" + "&session_key_only=true"
+
"&req_perms=read_stream,publish_stream,offline_access"
+ "&auth_token=" + facebookAuthToken;

// Here we launch a browser with the above URL so the user can
login to Facebook, grant our requested permissions and send our token
for pickup later
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();

if (desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(new URI(url));
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (URISyntaxException use) {
use.printStackTrace();
}
}
}
}

private static void facebookAuthorise() throws FacebookException {
// I saved my session key to a .properties file for future use
so the user doesn't keep have to authorising my app!
// auth.getSession returns JSON so we need to decode it just
to grab the session key
// http://wiki.developers.facebook.com/index.php/Auth.getSession
// Here we pickup the session key and define the authToken we
used above in facebookConnect()
JsonObject json = new
JsonObject(facebookClient.executeQuery("auth.getSession",
String.class, Parameter.with("auth_token", facebookAuthToken)));

facebookSessionKey = json.getString("session_key");

// An example call, you can literally use anything from the
REST API
Long uid =
facebookClient.executeQuery("users.getLoggedInUser",
facebookSessionKey, Long.class);
System.out.println("FB User ID: " + uid);
}
}

Marcelo

unread,
Feb 11, 2011, 5:01:48 PM2/11/11
to res...@googlegroups.com
Em 11/02/2011 14:59, kintu escreveu:
> I got this code can you help me?
> can you fix this for me so i get user's name and email

Granted, OAuth with Facebook is not the easiest thing to learn; most
tutorials on the web don't really work; and I had to struggle with it
for a day or two to get it working correctly with my architecture (gwt
on the client, facebook auth on the server).

But why do I have the feeling people don't really... try?


/Marcelo

ryuk

unread,
Jul 14, 2012, 6:05:25 AM7/14/12
to res...@googlegroups.com
the only thing that i can't understand is this!!:


facebookClient = new DefaultFacebookClient("2227470xxx| 2.PnVz9uj4blyh4iR_bGss4Q__.3600.1297386000-561331950|bNfq- xK8_Dyt2Ck1SY-g2xxxxxx");

how can i get this code?? "2227470xxx| 2.PnVz9uj4blyh4iR_bGss4Q__.3600.1297386000-561331950|bNfq- xK8_Dyt2Ck1SY-g2xxxxxx"

It should be the token but how is possible if the entire class is created to get this code??

Mark Allen

unread,
Jul 14, 2012, 2:22:12 PM7/14/12
to res...@googlegroups.com
RestFB cannot provide this.  To get an access token, you must use a mechanism like OAuth via the FB JS API.  There are docs all over the web for this - check FB and Google.

Thanks
Mark

Nick Fenwick

unread,
Jul 16, 2012, 12:10:34 AM7/16/12
to res...@googlegroups.com
On 07/14/2012 05:05 PM, ryuk wrote:
the only thing that i can't understand is this!!:

facebookClient = new DefaultFacebookClient("2227470xxx| 2.PnVz9uj4blyh4iR_bGss4Q__.3600.1297386000-561331950|bNfq- xK8_Dyt2Ck1SY-g2xxxxxx");

how can i get this code?? "2227470xxx| 2.PnVz9uj4blyh4iR_bGss4Q__.3600.1297386000-561331950|bNfq- xK8_Dyt2Ck1SY-g2xxxxxx"

You don't specify what type of application you're writing.  There are 7 different ways of authenticating a user (and getting their access token) given on https://developers.facebook.com/docs/authentication/

For example my app runs in a web server, which talks directly to Facebook, and so I use the server side flow described at https://developers.facebook.com/docs/authentication/server-side/ .. your needs may differ.  I do still have to direct my users to the Facebook web site to allow them to complete the OAuth Dialog.  Facebook then calls back on my app by returning them to the 'redirect_uri' URI given in the request.  It's not simple, don't expect this stuff to 'just work' without a lot of work on your part.  Marcelo, in another post, is not entirely fair when he implies you're not trying.. it's possible to try very hard and still be confused, just digging yourself deeper, until you understand all the different ways of doing things and find which path is right for your app.

Once you have a user's access token, give it to the DefaultFacebookClient constructor and make calls on behalf of that user .. but getting that users token isn't easy for beginners.

Can we presume that you are actually writing an App on facebook, and so you want to use your Application's access token?  lets not worry about user tokens for now.

http://www.restfb.com/ tells you:

Not sure how to get an OAuth access token?


Follow the link given, to: https://developers.facebook.com/tools/explorer

If you are logged in as the user who created the App you want the token for, you can use the dropdown at the top right which (for me) defaults to "Graph API Explorer".  Change this to the app you created.  The page will give you the Access Token for your app.

You can then use your App access token to make meaningful requests against Facebook to e.g. retrieve a user's information.



It should be the token but how is possible if the entire class is created to get this code??

You may be thinking about things inside-out.  To talk to the Facebook servers, every request (HTTP GET or POST etc) must be 'signed' with an access token, so they know who is asking.  restfb hides the complexity of the actual signing process from you.  All you have to to is give the restfb client object an access token, and it does the rest.

Or, to put it another way: you give the FacebookClient class the access token, so that when it talks to Facebook it can sign each request it sends.

By the way, your comment "but how do i implement web browser on java, java doesnt have web support like c#" is simply incorrect and misleading.  You aren't expected to implement a web browser (although you certainly can) .. most likely, you want to direct your users to a Facebook OAuth URL in their browser, allow them to complete the transaction with Facebook, and Facebook will call back on your server via HTTP.  How this is done depends on your app, which you haven't described.

Nick
Reply all
Reply to author
Forward
0 new messages