안드로이드
안드로이드 카카오 SDK V2 로그인 - part2(코드)
devGeek
2021. 9. 27. 22:43
반응형
안드로이드 카카오 SDK V2 로그인 - part2(코드)
1. 카카오 로그인 API 사용을 위한 설정
[내 애플리케이션] > [제품 설정] > [카카오 로그인] 에서 카카오 로그인 을 활성화 한다.
[내 애플리케이션] > [앱 설정] > [요약 정보] 에서 네이트브 앱 키를 복사해서 아래와 같이 작성한다.
코드 작성
- GlobalApplication.kt (Application을 상속받는 클래스) 생성한다.
아래와 같이 onCreate함수 내에 KakaoSdk.init()을 실행한다. class GlobalApplication: Application() { override fun onCreate() { super.onCreate() KakaoSdk.init(this, getString(R.string.kakao_app_key)) } }
- AndroidManifest.xml 파일에서 application 태그 속성 값으로
android:name=".GlobalApplication"
을 추가한다.
- 아래와 같이 AndroidManifest.xml 파일에서 application 태그 안에 코드를 추가하여 Redirect URI 설정한다.
(예를 들어 네이티브 앱 키가 '123456789'라면 'kakao123456789'를 입력한다.)
<activity android:name="com.kakao.sdk.auth.AuthCodeHandlerActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Redirect URI: "kakao{NATIVE_APP_KEY}://oauth" -->
<data android:host="oauth"
android:scheme="kakaoadf1f3ccd6e66fea62118ef585896c09" />
</intent-filter>
</activity>
- 화면에 버튼을 만든다.
[activity_main.xml]<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.constraintlayout.utils.widget.ImageFilterButton android:id="@+id/btn_kakao_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/kakao_login_large_narrow" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias=".7" /> </androidx.constraintlayout.widget.ConstraintLayout>
- 로그인 로직을 구현한다.
[MainActivity.kt]
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.utils.widget.ImageFilterButton
android:id="@+id/btn_kakao_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/kakao_login_large_narrow"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias=".7" />
</androidx.constraintlayout.widget.ConstraintLayout>
반응형