Mic*_*ith 78 android facebook facebook-graph-api facebook-android-sdk
标题说明了一切.我正在使用自定义按钮来获取用户的Facebook信息(用于"注册"目的).然而,我不希望应用程序记住最后一个注册用户,也不想通过Facebook本机应用程序当前登录的用户.我希望每次都能弹出Facebook登录活动.这就是为什么我想以编程方式注销任何以前的用户.
我怎样才能做到这一点?这是我登录的方式:
private void signInWithFacebook() {
SessionTracker sessionTracker = new SessionTracker(getBaseContext(), new StatusCallback()
{
@Override
public void call(Session session, SessionState state, Exception exception) {
}
}, null, false);
String applicationId = Utility.getMetadataApplicationId(getBaseContext());
mCurrentSession = sessionTracker.getSession();
if (mCurrentSession == null || mCurrentSession.getState().isClosed()) {
sessionTracker.setSession(null);
Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
Session.setActiveSession(session);
mCurrentSession = session;
}
if (!mCurrentSession.isOpened()) {
Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(RegisterActivity.this);
if (openRequest != null) {
openRequest.setPermissions(null);
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
mCurrentSession.openForRead(openRequest);
}
}else {
Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
fillProfileWithFacebook( user );
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我会在此方法的开头调用以注销任何以前的用户.
Min*_* Li 158
最新SDK的更新:
现在@ zeuter的答案对于Facebook SDK v4.7 +是正确的:
LoginManager.getInstance().logOut();
原始答案:
请不要使用SessionTracker.它是一个内部(包私有)类,并不打算作为公共API的一部分使用.因此,其API可能随时更改,无任何向后兼容性保证.您应该能够在代码中删除SessionTracker的所有实例,而只需使用活动会话.
要回答您的问题,如果您不想保留任何会话数据,只需在应用关闭时调用closeAndClearTokenInformation即可.
Rik*_*ati 87
此方法将帮助您在android中以编程方式从facebook注销
/**
* Logout From Facebook
*/
public static void callFacebookLogout(Context context) {
Session session = Session.getActiveSession();
if (session != null) {
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
//clear your preferences if saved
}
} else {
session = new Session(context);
Session.setActiveSession(session);
session.closeAndClearTokenInformation();
//clear your preferences if saved
}
}
Run Code Online (Sandbox Code Playgroud)
小智 65
自Facebook的Android SDK v4.0(请参阅changelog)以来,您需要执行以下操作:
LoginManager.getInstance().logOut();
Run Code Online (Sandbox Code Playgroud)
jpo*_*s18 27
这是允许我从facebook以编程方式注销的片段.如果您发现我可能需要改进的任何内容,请告诉我.
private void logout(){
// clear any user information
mApp.clearUserPrefs();
// find the active session which can only be facebook in my app
Session session = Session.getActiveSession();
// run the closeAndClearTokenInformation which does the following
// DOCS : Closes the local in-memory Session object and clears any persistent
// cache related to the Session.
session.closeAndClearTokenInformation();
// return the user to the login screen
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
// make sure the user can not access the page after he/she is logged out
// clear the activity stack
finish();
}
Run Code Online (Sandbox Code Playgroud)
Ani*_*kur 12
自Facebook的Android SDK v4.0起,您需要执行以下操作:
LoginManager.getInstance().logOut();
Run Code Online (Sandbox Code Playgroud)
这还不够.这会简单明确缓存的访问令牌和配置文件,以便AccessToken.getCurrentAccessToken()与Profile.getCurrentProfile()现在将成为空.
要完全注销,您需要撤消权限然后调用LoginManager.getInstance().logOut();.要撤消权限,请执行以下图API -
GraphRequest delPermRequest = new GraphRequest(AccessToken.getCurrentAccessToken(), "/{user-id}/permissions/", null, HttpMethod.DELETE, new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse graphResponse) {
if(graphResponse!=null){
FacebookRequestError error =graphResponse.getError();
if(error!=null){
Log.e(TAG, error.toString());
}else {
finish();
}
}
}
});
Log.d(TAG,"Executing revoke permissions with graph path" + delPermRequest.getGraphPath());
delPermRequest.executeAsync();
Run Code Online (Sandbox Code Playgroud)
SDK 4.0上已删除会话类.登录magement通过类LoginManager完成.所以:
mLoginManager = LoginManager.getInstance();
mLoginManager.logOut();
Run Code Online (Sandbox Code Playgroud)
作为参考升级到SDK 4.0说:
会话已删除 - AccessToken,LoginManager和CallbackManager类取代并替换Session类中的功能.
| 归档时间: |
|
| 查看次数: |
109879 次 |
| 最近记录: |