我正在创建一个全局Facebook对象(来自android-facebook-sdk),以便能够在我的活动*享它:
public class GlobalVars extends Application {
public static final String APP_ID = "123456789";
public Facebook facebook = new Facebook(APP_ID);
}
在其中一项活动中,我添加了LoginButton,如示例所示:
public class FacebookActivity extends Activity {
private LoginButton mLoginButton;
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFacebook = ((GlobalVars)getApplicationContext()).facebook;
setContentView(R.layout.facebook);
mLoginButton = (LoginButton) findViewById(R.id.login);
mText = (TextView) FacebookActivity.this.findViewById(R.id.txt);
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
SessionStore.restore(mFacebook, this);
SessionEvents.addAuthListener(new SampleAuthListener());
SessionEvents.addLogoutListener(new SampleLogoutListener());
mLoginButton.init(this, mFacebook);
}
...
}
现在,当我登录并使用相同的Facebook对象进入其他活动时:
public class MainActivity extends WhipemActivity {
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mFacebook = ((GlobalVars)getApplicationContext()).facebook;
}
....
}
并运行一个请求,例如:
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
mAsyncRunner.request("me/friends", new FriendRequestListener());
我得到一个
Facebook Error: Error validating access token.
怎么了?这是拥有全球Facebook对象的正确方法吗?
谢谢.
解决方法:
7月,在您的新活动中,您需要还原登录期间获得的访问令牌.
SessionStore.restore(mFacebook, this);
SessionStore保存并保存通过创建的SharedPreferences对象将访问令牌恢复到SharedPreferences中,或从中恢复.
SessionStore.restore()获取保存在SharedPreferences中的访问令牌,并将其设置在Facebook对象上.
您可以在“ Facebook示例简单”项目中查看,并查看SessionStore类以更好地了解SessionStore的工作方式.
facebook-android-sdk/examples/simple