我创建了一个long-lasting Facebook访问令牌。我有来自Facebook的TestApp的应用程序ID。问题是我似乎无法获得有效的会话!我做错了什么?
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));
}
}
已编辑的代码(我编辑了原始代码以反映给出的两个答案,但它仍然没有帮助):
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());
}
}
}
答案 0 :(得分:1)
使用SharedPreference在Android中存储会话
商品强>
SharedPreferences settings = context.getSharedPreferences("KEY_NAME",
context.MODE_PRIVATE);
Editor editor = settings.edit();
editor.putInt("fbsession", session);
editor.commit();
editor.apply();
的 Retreave 强>
SharedPreferences settings = context.getSharedPreferences("KEY_NAME",
context.MODE_PRIVATE);
String fbSession = settings.getInt("fbsession", 1);
答案 1 :(得分:0)
您可以像这样编码以在首选项中添加值
SharedPreferences prefs = context.getSharedPreferences("com.projectname.pkgname",
Context.MODE_PRIVATE);
prefs.edit().putString("facebook_accesstoken", "your_token").commit();
并且像这样
String facebook_accessstoken=prefs.getString("facebook_accesstoken", "");
答案 2 :(得分:0)
我认为我能想到的这种行为的唯一解释是我使用的是弃用的API。根据新API更改了我的代码后,它现在可以使用了。如果在Facebook文档中已经明确了这一点,我将不胜感激。
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");
}
}