Ronaldinho Learn Coding
Ronaldinho Learn Coding

Reputation: 13824

How to get permanent Facebook access token using java

If you using Facebook graph API, you can see all public information of someone if that person set those information public, only few things you need to do: open a browser, log-in to Facebook and know that person user ID#, for example, Mark Zuckerberg - facebook founder have user ID# 4, so go to: https://graph.facebook.com/4 => you can see basic info such as name, age, location .... (imagine that you change i in _https://graph.facebook.com/i with i from 1 to 1 million, you may see info of million user as long as they set their info in public)

or go to this link you can see more information such as person favorites and education ... _https://graph.facebook.com/4?access_token=XXX with XXX is a unique code, it changes every time you log-in to Facebook ( see more in http://developers.facebook.com/docs/reference/api/)

My goal is using basic java url to go to these link and get all info (basically they are text) and save them to a text file, with _https://graph.facebook.com/4 there is no problem but with second link problem is the access token code which is XXX - changes every time or will be expired after 20~30 minutes so I can't run my program longer to get more data. question is is there anyway to get a permanent access token or extend it for longer time, remember I only using a small java program, nothing to do with Facebook app or anything else. Thanks in advanced!

Upvotes: 2

Views: 4409

Answers (3)

Shatakshi
Shatakshi

Reputation: 11

You can extend the life of your access token to 60 days. You can go through this:

https://developers.facebook.com/docs/facebook-login/access-tokens/

If you make the following call from your server side, Facebook will return you an access token which will be valid for 2 months (extended access token):

GET /oauth/access_token?  
    grant_type=fb_exchange_token&           
    client_id={app-id}&
    client_secret={app-secret}&
    fb_exchange_token={short-lived-token} 

Upvotes: 1

Max Beikirch
Max Beikirch

Reputation: 2153

You cannot get a permanent access token, but it is quite easy to renew after its expiration: That article is the way to go. The magic URL is:

https://www.facebook.com/dialog/oauth/?client_id=YOUR_APP_ID&redirect_uri=YOUR_REDIRECT_URL&state=YOUR_STATE_VALUE&scope=COMMA_SEPARATED_LIST_OF_PERMISSION_NAMES
  • client_id
    The ID of your app. You must register a facebook app even though you are working "offline" with Java or such - but - there's no need to actually program the app. Just register the app so that you get a fixed app id (you need it here) and you'll be fine.
  • redirect_uri
    That's the important step - facebook redirects to the uri given in redirect_uri and adds an "#access_token=..." to that uri. You can parse that uri to acquire the token:

Call the link above with a webbrowser, use http://localhost as redirect_uri and you'll be fine. The browser will attempt to call http://localhost#access_token=.... and obviously fail, but you can parse the url to get the token. It is not a very straightforward process, but it works for me.

Upvotes: 0

George Yacoub
George Yacoub

Reputation: 1456

Unfortunately you can't get a permanent access token but instead you can catch the error when the access token is expired and request a new one. Also you might want to read facebook terms of use to make sure you are not violating it.

Upvotes: 2

Related Questions