Search code examples
javafacebookrestfb

RestFB: Using a facebook app to get the users Access Token


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?


Solution

  • 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();
        }
    
    }