Facebook 로그인 / 로그 아웃 버튼을 사용하지 않고 Facebook SDK 3.0에서 프로그래밍 방식으로 로그 아웃하는 방법은 무엇입니까?
제목에 모든 것이 나와 있습니다. 사용자의 페이스 북 정보를 가져 오기 위해 사용자 정의 버튼을 사용하고 있습니다 ( "가입"목적). 그러나 앱이 마지막으로 등록 된 사용자를 기억하고 싶지 않으며 현재 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 );
}
});
}
}
이상적으로는이 방법을 시작할 때 이전 사용자를 로그 아웃하도록 호출합니다.
최신 SDK 업데이트 :
이제 @zeuter의 대답은 Facebook SDK v4.7 +에 맞습니다.
LoginManager.getInstance().logOut();
원래 답변 :
SessionTracker를 사용하지 마십시오. 내부 (패키지 전용) 클래스이며 공용 API의 일부로 사용되지 않습니다. 따라서 API는 이전 버전과의 호환성 보장없이 언제든지 변경 될 수 있습니다. 코드에서 SessionTracker의 모든 인스턴스를 제거하고 대신 활성 세션을 사용할 수 있어야합니다.
질문에 답하려면 세션 데이터를 유지하지 않으려면 앱이 닫힐 때 closeAndClearTokenInformation을 호출 하면됩니다.
이 방법은 안드로이드에서 프로그래밍 방식으로 페이스 북에서 로그 아웃하는 데 도움이됩니다.
/**
* 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
}
}
Facebook의 Android SDK v4.0 ( changelog 참조 )부터 다음을 실행해야합니다.
LoginManager.getInstance().logOut();
다음은 내가 페이스 북에서 프로그래밍 방식으로 로그 아웃 할 수있게 해주는 스 니펫입니다. 개선해야 할 점이 있으면 알려주세요.
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();
}
Since Facebook's Android SDK v4.0 you need to execute the following:
LoginManager.getInstance().logOut();
This is not sufficient. This will simply clear cached access token and profile so that AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile() will now become null.
To completely logout you need to revoke permissions and then call LoginManager.getInstance().logOut();. To revoke permission execute following graph 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();
Session class has been removed on SDK 4.0. The login magement is done through the class LoginManager. So:
mLoginManager = LoginManager.getInstance();
mLoginManager.logOut();
As the reference Upgrading to SDK 4.0 says:
Session Removed - AccessToken, LoginManager and CallbackManager classes supercede and replace functionality in the Session class.
Yup, As @luizfelippe mentioned Session class has been removed since SDK 4.0. We need to use LoginManager.
I just looked into LoginButton class for logout. They are making this kind of check. They logs out only if accessToken is not null. So, I think its better to have this in our code too..
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if(accessToken != null){
LoginManager.getInstance().logOut();
}
private Session.StatusCallback statusCallback = new SessionStatusCallback();
logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Session.openActiveSession(this, true, statusCallback);
}
});
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state,
Exception exception) {
session.closeAndClearTokenInformation();
}
}
Facebook provides two ways to login and logout from an account. One is to use LoginButton and the other is to use LoginManager. LoginButton is just a button which on clicked, the logging in is accomplished. On the other side LoginManager does this on its own. In your case you have use LoginManager to logout automatically.
LoginManager.getInstance().logout() does this work for you.
'Program Club' 카테고리의 다른 글
| MySQL my.ini 위치 (0) | 2020.10.10 |
|---|---|
| Xcode : 체계 없음 (0) | 2020.10.10 |
| MACOS에서 Java를 9에서 8로 다운 그레이드하는 방법. (0) | 2020.10.10 |
| 자바 통화 번호 형식 (0) | 2020.10.10 |
| Vim 파일 탐색 (0) | 2020.10.10 |