I created a long-lasting Facebook access token. I have a TestApp's app ID from Facebook. The issue is that I can't seem to get a valid session! What am I doing wrong?
public class MainActivity extends Activity {
Facebook fb;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fb = new Facebook(getString(R.string.APP_ID));
fb.setAccessToken(getString(R.string.ACCESS_TOKEN));
//Expiry is set to 0, since the token never expires
fb.setAccessExpires(0);
if (fb.isSessionValid()) {
Toast.makeText(this, "Session is valid",
android.widget.Toast.LENGTH_LONG).show();
Log.d("TAG2", getString(R.string.ACCESS_TOKEN));
} else
Toast.makeText(this,
"Session is Invalid"+ getString(R.string.ACCESS_TOKEN),
android.widget.Toast.LENGTH_LONG).show();
Log.d("TAG3", getString(R.string.ACCESS_TOKEN));
}
}
Edited Code (I edited my original code to reflect the two answers given, but still it didn't help):
public class MainActivity extends Activity {
Facebook fb;
SharedPreferences prefs;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("AKEY",getString(R.string.ACCESS_TOKEN));
editor.commit();
editor.apply();
fb = new Facebook(getString(R.string.app_id));
fb.setAccessToken(prefs.getString("AKEY", ""));
//Expiry is set to 0, since the token never expires
fb.setAccessExpires(0);
if (fb.isSessionValid()) {
Toast.makeText(this, "Session is validd",
android.widget.Toast.LENGTH_LONG).show();
Log.d("TAG2", getString(R.string.ACCESS_TOKEN));
} else {
Toast.makeText(this, fb.toString(),
android.widget.Toast.LENGTH_LONG).show();
Log.d("TAG3", fb.toString() + fb.getAccessExpires()
+ "Access Token is: " + fb.getAccessToken() +prefs.getString("AKEY", "")
+ "Last update is:" + fb.getLastAccessUpdate()
+ "Session is:" + fb.getSession());
}
}
}
I think the only explaination for this behaviour that I could think is that I was using a deprecated API. After having changed my code according to the new API it works now. Would have appreciated it had this been made clear in the Facebook documentation.
public class MainActivity extends Activity {
SharedPreferences prefs;
Session session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("AKEY",getString(R.string.ACCESS_TOKEN));
editor.putLong("expire", 0);
editor.commit();
editor.apply();
Session.StatusCallback callback = new Session.StatusCallback()
{
@Override
public void call(
Session session,
SessionState state,
Exception exception)
{
// safety check
if (isFinishing())
return;
// check session state
if (state.equals(SessionState.CLOSED)
|| state.equals(SessionState.CLOSED_LOGIN_FAILED))
{
// clearFacebookInfoFromSharedPreferences();
Log.d("TAG A","clearFacebookInfoFromSharedPreferences()");
// specific action for when the session is closed
// because an open-session request failed
if (state.equals(SessionState.CLOSED_LOGIN_FAILED))
{
Log.d("TAG B","cancelProgressDialog()");
}
}
else if (state.equals(SessionState.OPENED))
{
// cancelProgressDialog();
Toast.makeText(MainActivity.this, "Succeeded connecting to Facebook", android.widget.Toast.LENGTH_LONG).show();
//handler.onSuccess();
}
}
};
if (Session.getActiveSession() == null
&& prefs.contains("AKEY")
&& prefs.contains("expire"))
{
// open a session from the access token info
// saved in the app's shared preferences
String accessTokenString = prefs.getString(
"AKEY",
"");
Date accessTokenExpires = new Date(prefs.getLong(
"expire",
0));
AccessToken accessToken = AccessToken.createFromExistingAccessToken(
accessTokenString,
accessTokenExpires,
null,
null,
null);
session=Session.openActiveSessionWithAccessToken(this, accessToken, callback);
if (session != null) {
Toast.makeText(this, "Session is valid",
android.widget.Toast.LENGTH_LONG).show();
Log.d("TAG2", ""+session.getExpirationDate()+session.getAccessToken());
} else {
Toast.makeText(this, "Session is invalid",
android.widget.Toast.LENGTH_LONG).show();
}
Bundle params = new Bundle();
params.putString("fields", "likes.limit(1).summary(true)");
Request request = new Request(session, "922169881132606", params, HttpMethod.GET, new Request.Callback() {
@Override
public void onCompleted(Response response) {
try {
Log.d("TAG5", "In Try block");
GraphObject go = response.getGraphObject();
Log.d("graph object is", go.toString());
JSONObject obj = go.getInnerJSONObject();
JSONObject arr = obj.getJSONObject("likes");
JSONObject res = arr.getJSONObject("summary");
Log.d("TAG23", "JSON Set");
int numberOfLikes = res.getInt("total_count");
Log.d("Number of likes is ", Integer.toString(numberOfLikes));
Toast.makeText(MainActivity.this, "Number of likes is",
android.widget.Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.d("An error was thrown","alpha"+e);
e.printStackTrace();
}
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
Log.d("TAG7", "task is executed");
}
}