Skip to content

Authentification

Get Request Token

TMDb.init(YOUR_API_KEY, V4_AUTH_KEY)

suspend fun getRequestToken(): String? {
    val response: TmdbMessage.RequestToken = 
        TMDb.authService.requestToken().invoke()

    if (response != null && response.success)
        return response.requestToken

    return null
}

Afterwards, you have to validate this request token (giving permission) by redirecting the user to the auth url TMDb.authLink(requestToken).

Get Access Token

To access the user’s private lists or modify them a setted access token is required. You should set the access token alongside the setup at start (in Android in the App’s onCreate implementation).

suspend fun setAccessToken(requestToken: String): Boolean {
    val response: TmdbMessage.AccessToken =
        TMDb.accessToken(requestToken).invoke()

    if (response != null && response.success) {
        // Save the access token to the TMDb instance
        TMDb.accessToken = response.accessToken
    }

    return response?.success ?: false
}

To keep the user logged in on app start, you should set the access token alongside the setup at start (in Android in the App’s onCreate implementation).

Get Session ID

Similar to the access token for lists for retrieving the user’s account data (general info, favorite lists etc) a session id is required. It can be easily generated using the access token which is previously fetched.

suspend fun setSessionId(accessToken: String): String? {
    val response: TmdbMessage.SessionId =
        TMDb.accountService.accessTokenToSessionID(accessToken).invoke()

    if (response != null && response.success) {
        // Save the access token to the TMDb instance
        TMDb.sessionId = response.sessionId
    }

    return response?.sessionId
}

Account

Get account info

suspend fun getAccount(): TmdbAccount? {
    // Make sure that the session id is set in the TMDb instance before the call
    val account: TmdbAccount = TMDb.accountService.details().invoke()
    return account
}

Examples

Fetch favorite TV shows

suspend fun getShowPage(accountId: Int, page: Int = 1): TmdbPage<TmdbShow.Slim>? {
    // Make sure that the session id is set in the TMDb instance before the call
    val page: TmdbPage<TmdbShow.Slim>: =  TMDb.favoriteShows(accountId, page = page).invoke()
    return page
}