11

This is what i have:

static AccessToken accessToken = new DefaultFacebookClient().obtainExtendedAccessToken("<my app id>", "<my app secret>");
static FacebookClient client = new DefaultFacebookClient();
public static void main(String args[]) {
    System.out.print("Enter Your Status: ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String status= null;

      try {
         userName = br.readLine();
         System.out.println("..........");
      } catch (IOException ioe) {
         System.out.println("!");
         System.exit(1);
      }

    FacebookType publishMessageResponse =
                client.publish("me/feed", FacebookType.class,
                Parameter.with("message", status));

So first line gets the token and stores it as type AccessToken but what good does that do to me because next line i need to provide the access token as a string and i can't convert it. Any Help?

Kenny Cason
  • 12,109
  • 11
  • 47
  • 72
Bevilacqua
  • 465
  • 3
  • 8
  • 19

5 Answers5

11

First of all don't confuse app token with user token (more info)

To get user token you have to

  1. Provide a way for user to authenticate against Facebook (more info) and receive "code" - an encrypted string unique to each login request.
  2. Get the user token using this code.

You can get user token with pure RestFB the following way:

private FacebookClient.AccessToken getFacebookUserToken(String code, String redirectUrl) throws IOException {
    String appId = "YOUR_APP_ID";
    String secretKey = "YOUR_SECRET_KEY";

    WebRequestor wr = new DefaultWebRequestor();
    WebRequestor.Response accessTokenResponse = wr.executeGet(
            "https://graph.facebook.com/oauth/access_token?client_id=" + appId + "&redirect_uri=" + redirectUrl
            + "&client_secret=" + secretKey + "&code=" + code);

    return DefaultFacebookClient.AccessToken.fromQueryString(accessTokenResponse.getBody());
}

The call is simple:

FacebookClient.AccessToken token = getFacebookUserToken(code, redirectUrl);
String accessToken = token.getAccessToken();
Date expires = token.getExpires();
Val
  • 432
  • 5
  • 11
  • can you please mention how to get that code.. small example or something? – Vijay Mar 21 '14 at 19:06
  • @Vijay, There's a link in Paragraph 1 of my answer for more details. For example: https://www.facebook.com/dialog/oauth?client_id=123&scope=email,user_birthday&display=popup&redirect_uri=myuri – Val Mar 22 '14 at 00:10
  • Exactly!, to get code, can using Scribe-Java : https://github.com/fernandezpablo85/scribe-java/blob/master/src/test/java/org/scribe/examples/FacebookExample.java – searching9x Jun 19 '14 at 16:14
  • please describe better how to get the `code` parameter - the facebook doc only yields a way to do so by opening a dialog. I'd like to do it in app if the credentials are given by a backend property-file or something – xetra11 Oct 07 '16 at 12:20
9

In addition to what Jack said about AccessToken.getAccessToken() returning the string value of accessToken, you can avoid instantiating DefaultFacebookClient twice by extending DefaultFacebookClient like this:

import com.restfb.DefaultFacebookClient;

public class LoggedInFacebookClient extends DefaultFacebookClient {

    public LoggedInFacebookClient(String appId, String appSecret) {
        AccessToken accessToken = this.obtainAppAccessToken(appId, appSecret);
        this.accessToken = accessToken.getAccessToken();
    }

}
Kenny Cason
  • 12,109
  • 11
  • 47
  • 72
5

Try the following code:

AccessToken accessToken = new DefaultFacebookClient().obtainAppAccessToken(appid,appsecret);
String token=accessToken.getAccessToken();
Mostafa Berg
  • 3,211
  • 22
  • 36
user2492472
  • 51
  • 1
  • 1
  • I tried this code but get the followingexception Exception in thread "main" com.restfb.exception.FacebookOAuthException: Received Facebook error response of type OAuthException: Error validating client secret. (code 1, subcode null) – lulu Feb 27 '14 at 03:13
1

Per restfb.FacebookClient.AccessToken, you should be able to call accessToken.getAccessToken() -- that should return the String you are looking for.

Jack White
  • 11
  • 1
0

This will work

AccessToken accessToken = new DefaultFacebookClient().obtainAppAccessToken("XXXX", "XXXX");
String token=accessToken.getAccessToken();
DefaultFacebookClient facebookClient = new DefaultFacebookClient(token);
Dilip Bobby
  • 323
  • 5
  • 15