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:
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