ChamomileGuides 3.0.4 Help

네이버 로그인(AOS)

개요

네이버 로그인은 OAuth 2.0 기반의 사용자 인증 기능을 제공해, 네이버가 아닌 다른 서비스에서 네이버의 사용자 인증 기능을 이용할 수 있게 하는 서비스다. 별도의 아이디나 비밀번호를 기억할 필요 없이 네이버 아이디로 간편하고 안전하게 서비스에 로그인할 수 있다.

로그인 과정

file

구현

(1) Naver Developers 세팅
(2) 프로젝트 설정
(3) 서비스 구현
(4) 로그인 테스트

  1. 네이버 디벨로퍼 홈페이지 로그인 https://developers.naver.com

  2. 각 항목들을 입력하고 애플리케이션을 등록한다.

    file

    • 내 앱에 필요한 필수 정보와 선택 정보를 체크한다.

      file
      file

    • 다운로드 url이 없으면 https://www.naver.com 이런식으로 입력해도 무방하다.

      file

  3. Client ID, Client Secret은 프로젝트 설정할 때 필요하니, 기억 해둔다.

    file

프로젝트 설정

  1. build.gradle (Module) implementation 'com.navercorp.nid:oauth-jdk8:5.1.0' 추가

    file

서비스 구현

  1. SDK 초기화 (아래 전체 코드에 포함)

    • naverClientId - 애플리케이션 등록 후 발급받은 클라이언트 아이디

    • naverClientSecret - 애플리케이션 등록 후 발급받은 클라이언트 시크릿

    • naverClientName - 네이버 앱의 로그인 화면에 표시할 애플리케이션 이름. 모바일 웹의 로그인 화면을 사용할 때는 서버에 저장된 애플리케이션 이름이 표시

  2. 네이버로그인 모듈 구현 (전체 코드)

    class SocialLoginController (private val basicAct: AppCompatActivity) { var mInitialized = false var m_action: FlexAction? = null private fun initialize() { getActivity()?.let { if (!mInitialized) { /** Naver Login Module Initialize */ val naverClientId = ActivityProvider.mainActivity!!.getString(R.string.social_login_info_naver_client_id_twk) // 애플리케이션 등록 후 발급받은 클라이언트 아이디 val naverClientSecret = ActivityProvider.mainActivity!!.getString(R.string.social_login_info_naver_client_secret_twk) // 애플리케이션 등록 후 발급받은 클라이언트 시크릿 val naverClientName = ActivityProvider.mainActivity!!.getString(R.string.social_login_info_naver_client_name_twk) // 네이버 앱의 로그인 화면에 표시할 애플리케이션 이름. 모바일 웹의 로그인 화면을 사용할 때는 서버에 저장된 애플리케이션 이름이 표시된다. NaverIdLoginSDK.initialize(App.context, naverClientId, naverClientSecret , naverClientName) mInitialized = true } } } fun startSocialLogin(loginType: DEFINES.SOCIAL_LOGIN.TYPE){ if(!mInitialized) initialize() when(loginType){ DEFINES.SOCIAL_LOGIN.TYPE.naver -> { startNaverLogin() } } } val socialLogin = FlexLambda.action { action, array: FlexArguments -> m_action = action withContext(Dispatchers.Main) { when(array[0]?.asString()){ "naver" -> { startSocialLogin(DEFINES.SOCIAL_LOGIN.TYPE.naver) } } } } /** 네이버 */private fun startNaverLogin(){ var naverToken :String? = "" val profileCallback = object : NidProfileCallback<NidProfileResponse> { override fun onSuccess(response: NidProfileResponse) { val userId = response.profile?.id onLoginCompleted(DEFINES.SOCIAL_LOGIN.TYPE.naver, userId, naverToken!!,null) } override fun onFailure(httpStatus: Int, message: String) { val errorCode = NaverIdLoginSDK.getLastErrorCode().code val errorDescription = NaverIdLoginSDK.getLastErrorDescription() onError(DEFINES.SOCIAL_LOGIN.TYPE.naver, Error("errorCode:$errorCode, errorDesc:$errorDescription")) } override fun onError(errorCode: Int, message: String) { onFailure(errorCode, message) } } /** OAuthLoginCallback을 authenticate() 메서드 호출 시 파라미터로 전달하거나 NidOAuthLoginButton 객체에 등록하면 인증이 종료되는 것을 확인할 수 있습니다. */ val oauthLoginCallback = object : OAuthLoginCallback { override fun onSuccess() { // 네이버 로그인 인증이 성공했을 때 수행할 코드 추가 naverToken = NaverIdLoginSDK.getAccessToken() //로그인 유저 정보 가져오기 NidOAuthLogin().callProfileApi(profileCallback) } override fun onFailure(httpStatus: Int, message: String) { val errorCode = NaverIdLoginSDK.getLastErrorCode().code val errorDescription = NaverIdLoginSDK.getLastErrorDescription() onError(DEFINES.SOCIAL_LOGIN.TYPE.naver, Error("errorCode:$errorCode, errorDesc:$errorDescription")) } override fun onError(errorCode: Int, message: String) { onFailure(errorCode, message) } } NaverIdLoginSDK.authenticate(basicAct, oauthLoginCallback) } private fun onLoginCompleted(loginType: DEFINES.SOCIAL_LOGIN.TYPE, userId: String?, accessToken: String?, error: Error?){ Toast.makeText(App.context,"로그인 완료 userId : $userId , accessToken : $accessToken", Toast.LENGTH_SHORT).show() val returnData = JSONObject() returnData.put("userId", userId) returnData.put("accessToken", accessToken) val returnObj = Utils.returnJson(authValue = true, dataValue = returnData, msgValue = null) m_action?.promiseReturn(returnObj) } private fun onError(loginType : DEFINES.SOCIAL_LOGIN.TYPE, error : Error?){ Toast.makeText(App.context,"로그인 중 에러발생 ${error.toString()}", Toast.LENGTH_SHORT).show() val returnObj = Utils.returnJson(authValue = false, dataValue = null, msgValue = "로그인 중 에러발생 ${error.toString()}") m_action?.promiseReturn(returnObj) } }

로그인 테스트

file
file

참고 자료

https://developers.naver.com/docs/login/android/android.md

Last modified: 21 4월 2025