Created
January 20, 2023 11:22
-
-
Save joaomneto/c58a8eb506c55a0065dc47f80c0fdb7c to your computer and use it in GitHub Desktop.
trakt api pull ratings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.ktor.client.HttpClient | |
import io.ktor.client.request.header | |
import io.ktor.client.request.post | |
import io.ktor.http.HttpHeaders | |
import io.ktor.http.formUrlEncode | |
suspend fun generateAccessToken(clientId: String, clientSecret: String, username: String, password: String): String { | |
val client = HttpClient() | |
val response = client.post<String>("https://api.trakt.tv/oauth/token") { | |
header(HttpHeaders.Authorization, "Basic ${base64Encode("$clientId:$clientSecret")}") | |
header("Content-Type", "application/x-www-form-urlencoded") | |
body = formUrlEncode( | |
"grant_type" to "password", | |
"username" to username, | |
"password" to password | |
) | |
} | |
val accessToken = response.parseJsonObject().get("access_token").toString() | |
return accessToken | |
} | |
suspend fun getMovieRatings(accessToken: String, page: Int) { | |
val client = HttpClient() | |
val ratings = client.get<String>("https://api.trakt.tv/users/me/ratings/movies") { | |
header(HttpHeaders.Authorization, "Bearer $accessToken") | |
header("trakt-api-version", "2") | |
header("Content-Type", "application/json") | |
header("page", page) | |
} | |
println(ratings) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment