Android SDK for Kakao Open API written in coroutines.
Coroutines is officially released with Kotlin 1.3 and now becoming more and more popular. Many developers are turning to coroutines to get rid of callback-based async code or over-spec RxJava with high learning curve. Kakao Android SDK is re-written with Coroutines to achieve
Not released yet. Pasting below code wouldn't work for now. This SDK is modularized as below:
- auth-coroutines
- user-coroutines
- talk-coroutines
- story-coroutines
- link-coroutines
- plusfriend-coroutines
- kakaonavi-coroutines
dependencies {
implementation 'com.kakao:sdk:user-coroutines:0.1.0'
implementation 'com.kakao.sdk:link-coroutines:0.1.0'
...
}
It is recommended to use the same sdk version among modules so that there is no version conflict.
This SDK requires the following transitive dependencies (other than coroutines):
- Gson
- Retrofit (2.6.0)
Above are minimum dependencies to get rid of repetitive json parsing and boilerplate network code.
You have to initialize SDK before you call any of the APIs. Initializing SDK is as simple as below:
class KakaoApplication : Application() {
override fun onCreate() {
super.onCreate()
KakaoSdkProvider.applicationContextInfo =
ApplicationContextInfo(
context = this,
clientId = "dd4e9cb75815cbdf7d87ed721a659baf")
}
}
You have to provide application context at initialization so that SDK can find necessary metadata with it. It is conventional to initialize SDK in Application#onCreate() method since it is guaranteed to be run before any part of your application code, but this is not a strict cosntraint.
First, users have to get access token in order to call Kakao API. Access tokens are issued according to OAuth 2.0 spec.
- kakao account authentication
- user agreement (skip if not necessary)
- authorization codem (via redirect)
- issue access token (via POST API)
Sample login code is pasted below:
fun loginButtonClicked() = launch {
val code = AuthCodeService.instance.requestAuthCode(this@LoginActivity)
withContext(Dispatchers.IO) {
AuthApiClient.instance.issueAccessToken(authCode = code)
}
val mainIntent = Intent(this@LoginActivity, MainActivity::class.java)
mainIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(mainIntent)
}
After ensuring that access token does exist with above step, you can call token-based API. Below are set of APIs that are currently supported by this SDK.
- UserApiClient
- TalkApiClient
- StoryApiClient
Below is an example of calling /v2/user/me API with UserApi class.
Kakao SDK with Coroutines support customizations in many layers.