What is the code to get an updated Facebook token in an Android app?

I am developing an Android application. With the upcoming obsolescence of the offline_access Facebook permission, I'm trying to use the Graph API to extend the Facebook token.

https://graph.facebook.com/oauth/access_token? client_id=APP_ID& client_secret=APP_SECRET& grant_type=fb_exchange_token& fb_exchange_token=EXISTING_ACCESS_TOKEN 

Can someone provide a detailed code demonstrating how to implement this code in an Android application and get an updated Facebook token?

Thank you for your time.

Update two:

Progress (I think)!

Using the full url with the facebook request method causes the base url to be added to the top of the url so instead

 String refreshUrl = "https://graph.facebook.com/oauth/access_token?client_id=12345678910&client_secret=abcdefghijklmnopqrstuvwxyz&grant_type=fb_exchange_token&fb_exchange_token="+currentAccessToken; 

should use

 String refreshUrl = "oauth/access_token?client_id=12345678910&client_secret=abcdefghijklmnopqrstuvwxyz&grant_type=fb_exchange_token&fb_exchange_token="+currentAccessToken; 

However, now I get the response {"error": {"message": "Application verification failed. Invalid application identifier.", "Type": "OAuthException", "code": 190}}

Update one:

Here is what I tried. The code ends, that is, OnComplete on Listerner is called, BUT the response does not contain a new access token or expiry value.

  void refreshWithGraph() { AsyncFacebookRunner extendingAsyncRunner = new AsyncFacebookRunner(facebook); Bundle parameters = new Bundle(); //the variable currentAccessToken is obtained after authorisation is complete using currentAccessToken = facebook.getAccessToken(); String refreshUrl = "https://graph.facebook.com/oauth/access_token?client_id=12345678910&client_secret=abcdefghijklmnopqrstuvwxyz&grant_type=fb_exchange_token&fb_exchange_token="+currentAccessToken; extendingAsyncRunner.request(refreshUrl, parameters, new RefreshListener(), null ); } 

Here is my version of RefreshListener ...

  //REFRESH LISTENER public class RefreshListener extends BaseRequestListener { public void onComplete(final String response, Object state) { try { final JSONObject json = Util.parseJson(response); runOnUiThread(new Runnable() { @Override public void run() { tvRefreshResponse.setText("IN REFRESH LISTENER ONCOMPLETE\nResponse is " + response); tvRefreshToken.setText("IN REFRESH LISTENER ONCOMPLETE\nToken is " + facebook.getAccessToken()); tvRefreshExpiry.setText("IN REFRESH LISTENER ONCOMPLETE\nFacebook expiry is " + millisecToDate(facebook.getAccessExpires())); } }); //end runOnUiThread } catch (JSONException e) { runOnUiThread(new Runnable() { @Override public void run() { tvRefreshResponse.setText("IN REFRESH LISTENER ONCOMPLETE CAUGHT JSON EXCEPTION \nResponse is " + response); } }); //end runOnUiThread } catch (FacebookError fe) { runOnUiThread(new Runnable() { @Override public void run() { tvRefreshResponse.setText("IN REFRESH LISTENER ONCOMPLETE CAUGHT FACEBOOK ERROR \nResponse is " + response); } }); //end runOnUiThread } //end catch Facebook error } //end onComplete @Override public void onIOException(IOException e, Object state) { tvRefreshResponse.setText("IN REFRESH LISTENER IOEXCEPTION \nException is "+ e.getLocalizedMessage()); } @Override public void onFileNotFoundException(FileNotFoundException e, Object state) { tvRefreshResponse.setText("IN REFRESH LISTENER FILE NOT FOUND EXCEPTION \nException is "+ e.getLocalizedMessage()); } @Override public void onMalformedURLException(MalformedURLException e, Object state) { tvRefreshResponse.setText("IN REFRESH MALFORMED URL \nException is "+ e.getLocalizedMessage()); } @Override public void onFacebookError(FacebookError e, Object state) { tvRefreshResponse.setText("IN REFRESH ONFACEBOOK ERROR \nException is "+ e.getLocalizedMessage()); } } //end RefreshListener 

The code ends, that is, OnComplete on Listerner is called, BUT the response does not contain a new access token or expiration value. Answer...

  {"id":"https:\/\/graph.facebook.com\/oauth\/access_token","shares":78,"comments":1} 

When I put the same URL (with the alphanumeric current value of the token) in a web browser, the response includes an access token.


Additional Information

Facebook offline_access permission will be deprecated on May 1, 2012.

Please do not suggest using the extendAccessTokenIfNeeded function in onResume () instead. I also have problems with this, and it is for this reason that I am looking at Graph API tokens :-)

Related Questions

Is it possible to renew Facebook tokens with the extension AccessTokenIfNeeded in an Android application? (my question)

How will autonomous_load work after expiration after May 1?

Facebook access token cannot be extended

Application privacy protection for using extendedAccessToken (Java / Android)

Related Facebook Links

Facebook Android Tutorial

Facebook offline_access permission denial

+8
android facebook facebook-android-sdk facebook-access-token
source share
1 answer

Honestly, I'm a little confused - it looks like you have everything to do it - and it's simple. But let me try to answer your question. Here is the code from my C # project where I extend the application token with my comments if you are not familiar with C # languages ​​and classes:

 string currentToken = "token from somewhere"; // WebClient is used to send GET request to the given URL // and receive the response using (var req = new System.Net.WebClient()) { // create URL string and fill it in with data (app Id, secret, and current token) var extendTokenUrl = string.Format( "https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type=fb_exchange_token&fb_exchange_token={2}", FB_APP_ID, FB_APP_SECRET, currentToken); // send GET request and download the response var response = req.DownloadString(extendTokenUrl); // if all is good, response will be a string which looks like this: // access_token=<<THE TOKEN GOES HERE>> var newToken = response.Substring("access_token=".Length); // now save newToken in your DB INSTEAD of currentToken - // so all calls will be made with extended token SaveTokenInDB(newToken); } 

hope this helps, and translating this into Java should be easy.

+7
source share

All Articles