페이지 이동경로
  • Docs>
  • Kakao Login

Kakao Login

Legacy Android

This document describes how to integrate Kakao Login APIs into your service with the Legacy Kakao SDK for Android (hereinafter Legacy Android SDK).

Before you begin

To integrate Kakao Login with the SDK for Android, open the activity_main.xml file and add LoginButton in the Activity layout. The following is an example of arranging the Login button at the bottom center of the screen by using ConstraintLayout.

Sample: activity_login.xml
<com.kakao.usermgmt.LoginButton
    android:id="@+id/login_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="30dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent" />

Login

This API enables users to log in through Kakao Talk or their Kakao Account information. This is an example of the LoginActivity class in which you can implement the login activity.

Sample: LoginActivity
public class LoginActivity extends Activity {

    // Implement session callback 
    private ISessionCallback sessionCallback = new ISessionCallback() {
        @Override
        public void onSessionOpened() {
            Log.i("KAKAO_SESSION", "Login Success ");
        }

        @Override
        public void onSessionOpenFailed(KakaoException exception) {
            Log.e("KAKAO_SESSION", "Login Fail", exception);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // Add session callback 
        Session.getCurrentSession().addCallback(sessionCallback);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // Delete session callback 
        Session.getCurrentSession().removeCallback(sessionCallback);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        // Pass the result of Simple login through Kakao Talk or Kakao Story to SDK 
        if (Session.getCurrentSession().handleActivityResult(requestCode, resultCode, data)) {
            return;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}

The most crucial part of the login activity is the session process. You need to implement appropriate actions that proceed after login, such as checking the session status and adding or deleting callback to proceed with the login result.

In response to the request, two types of tokens return: an access token and a refresh token. Refer to Token Management or Concepts to learn more about tokens.

To maintain the login session

tokens that are issued using the getCurrentSession().checkAndImplicitOpen() method maintains the login session. Using this method also starts the session, refreshs tokens, or notifies the inability to maintain the login state after checking if the token is available. Note that the actual session is processed with the ISessionCallback method.

Advanced guide: Setting identification method

The Kakao Login on Android starts with the dialog when a user touches the login button so that the user can select a verification method. If you want the user to use a specific verification method, not allowing them to select, set getAuthTypes to one of AuthTypes when initializing the SDK in the ISessionConfig.

For example, if you want users to log in only through Kakao Talk, set getAuthTypes to AuthType.KAKAO_TALK_ONLY. You can also set whether to encrypt tokens. See the sample code below.

ISessionConfig
Name Return Description
getAuthTypes AuthType User identification method
If not specified, all options are available.
isUsingWebviewTimer Boolean Whether to set a timer to pause or resume in the login web view to reduce CPU usage
If set to "true", you should set a timer in all Kakao Login web views.
(Default: false)
isSecureMode Boolean Whether to encrypt an access token and a refresh Token during the login process
isSaveFormData Boolean Whether to save the data entered in the email entry field in the login web view
(Default: true)
AuthType (Enum class)
Name Value Description
KAKAO_TALK 0 Login through Kakao Talk
KAKAO_STORY 1 Login through Kakao Story
KAKAO_ACCOUNT 2 Login with Kakao Account in the web view
KAKAO_LOGIN_ALL 4 Login with any methods
KAKAO_TALK_ONLY 5 Login through Kakao Talk
If the login fails, it does not proceed in the web view page (anymore).
Sample
    public ISessionConfig getSessionConfig() {
            return new ISessionConfig() {
                @Override
                public AuthType[] getAuthTypes() {
                    return new AuthType[] {AuthType.KAKAO_LOGIN_ALL};
                }

                @Override
                public boolean isUsingWebviewTimer() {
                    return false;
                }

                @Override
                public boolean isSecureMode() {
                    return false;
                }

                @Override
                public ApprovalType getApprovalType() {
                    return ApprovalType.INDIVIDUAL;
                }

                @Override
                public boolean isSaveFormData() {
                    return true;
                }
            };
        }

Logout

This API expires the access and refresh token issued through the login process to have a user log out. Implement the LogoutResponseCallback to receive the result of the logout request and make the requestLogout call. If successful, the Kakao SDK deletes the tokens and has the login session end.

Sample
UserManagement.getInstance()
        .requestLogout(new LogoutResponseCallback() {
            @Override
            public void onCompleteLogout() {
                Log.i("KAKAO_API", "Logout completed!");
            }
        });

Unlink

This API unlinks a user with an app. Implement the UnlinkResponseCallback according to the result of unlink request and make the UserManagement#requestUnlink call. If successful, the user is logged out and the login session is ended. When the user tries logging in the app after the unlink, the user needs to give consent and link the account with the app again.

Parameter: UnlinkResponseCallback
Name Type Description
onSuccess Long Service user ID that has been successfully unlinked with the service.
onSessionClosed ErrorResult If the session is ended and failed, an error message returns.
onFailure ErrorResult If the unlink fails, an error message returns.
Sample
UserManagement.getInstance()
        .requestUnlink(new UnLinkResponseCallback() {
            @Override
            public void onSessionClosed(ErrorResult errorResult) {
                Log.e("KAKAO_API", "Session is closed: " + errorResult);
            }

            @Override
            public void onFailure(ErrorResult errorResult) {
                Log.e("KAKAO_API", "Unlink fails: " + errorResult);

            }
            @Override
            public void onSuccess(Long result) {
                Log.i("KAKAO_API", "Unlink succeeds. id: " + result);
            }
        });

Token management

The Kakao SDK for Android has a built-in token management feature and allows you to refresh the tokens by requesting Kakao API. You can also request the token information if needed, as exemplified below.

Sample
AuthService.getInstance()
        .requestAccessTokenInfo(new ApiResponseCallback<AccessTokenInfoResponse>() {
            @Override
            public void onSessionClosed(ErrorResult errorResult) {
                Log.e("KAKAO_API", "Session is closed: " + errorResult);
            }

            @Override
            public void onFailure(ErrorResult errorResult) {
                Log.e("KAKAO_API", "Token information request fails: " + errorResult);
            }

            @Override
            public void onSuccess(AccessTokenInfoResponse result) {
                Log.i("KAKAO_API", "Service user ID: " + result.getUserId());
                Log.i("KAKAO_API", "Time remaining(s): " + result.getExpiresIn());
            }
        });
getExpiresInMillis()

To maintain consistency in parameter names and units in a REST API and the Kakao SDK, we changed 'getExpiresInMillis()' to 'getExpiresIn()'.

See more