Compare commits

..

No commits in common. "eccbc942694e08d6af02d8a199c3434f996074df" and "5e3a350e15e1e07b5b8168b1bba0d8ae00d6fbeb" have entirely different histories.

3 changed files with 6 additions and 13 deletions

View File

@ -11,7 +11,6 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import space.mori.chzzk_bot.chatbot.chzzk.Connector.getChannel import space.mori.chzzk_bot.chatbot.chzzk.Connector.getChannel
import space.mori.chzzk_bot.chatbot.discord.Discord import space.mori.chzzk_bot.chatbot.discord.Discord
import space.mori.chzzk_bot.chatbot.utils.refreshAccessToken
import space.mori.chzzk_bot.common.events.* import space.mori.chzzk_bot.common.events.*
import space.mori.chzzk_bot.common.models.User import space.mori.chzzk_bot.common.models.User
import space.mori.chzzk_bot.common.services.LiveStatusService import space.mori.chzzk_bot.common.services.LiveStatusService
@ -223,10 +222,7 @@ class UserHandler(
throw RuntimeException("AccessToken or RefreshToken is not valid.") throw RuntimeException("AccessToken or RefreshToken is not valid.")
} }
try { try {
val tokens = user.refreshToken?.let { token -> Connector.client.refreshAccessToken(token) } val tokens = Connector.client.refreshAccessToken(user.refreshToken!!)
if(tokens == null) {
throw RuntimeException("AccessToken is not valid.")
}
client = Connector.getClient(tokens.first, tokens.second) client = Connector.getClient(tokens.first, tokens.second)
UserService.setRefreshToken(user, tokens.first, tokens.second) UserService.setRefreshToken(user, tokens.first, tokens.second)
chatChannelId = getChzzkChannelId(channel.channelId) chatChannelId = getChzzkChannelId(channel.channelId)

View File

@ -1,12 +1,9 @@
package space.mori.chzzk_bot.chatbot.utils package space.mori.chzzk_bot.chatbot.utils
import com.google.gson.Gson import com.google.gson.Gson
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import space.mori.chzzk_bot.chatbot.chzzk.dotenv
import space.mori.chzzk_bot.common.utils.client import space.mori.chzzk_bot.common.utils.client
import xyz.r2turntrue.chzzk4j.ChzzkClient import xyz.r2turntrue.chzzk4j.ChzzkClient
import java.io.IOException import java.io.IOException
@ -32,16 +29,16 @@ data class RefreshTokenResponse(
) )
fun ChzzkClient.refreshAccessToken(refreshToken: String): Pair<String, String> { fun ChzzkClient.refreshAccessToken(refreshToken: String): Pair<String, String> {
val url = "https://openapi.chzzk.naver.com/auth/v1/token" val url = "https://chzzk.naver.com/auth/v1/token"
val request = Request.Builder() val request = Request.Builder()
.url(url) .url(url)
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.post(gson.toJson(mapOf( .post(gson.toJson(mapOf(
"grantType" to "refresh_token", "grantType" to "refresh_token",
"refreshToken" to refreshToken, "refreshToken" to refreshToken,
"clientId" to dotenv["NAVER_CLIENT_ID"], "clientId" to this.apiClientId,
"clientSecret" to dotenv["NAVER_CLIENT_SECRET"] "clientSecret" to this.apiSecret
)).toRequestBody("application/json; charset=utf-8".toMediaType())) )).toRequestBody())
.build() .build()
client.newCall(request).execute().use { response -> client.newCall(request).execute().use { response ->

View File

@ -181,7 +181,7 @@ val server = embeddedServer(Netty, port = 8080, ) {
clientSecret = dotenv["NAVER_CLIENT_SECRET"] clientSecret = dotenv["NAVER_CLIENT_SECRET"]
) )
val response = applicationHttpClient.post("https://openapi.chzzk.naver.com/auth/v1/token") { val response = applicationHttpClient.post("https://chzzk.naver.com/auth/v1/token") {
contentType(ContentType.Application.Json) contentType(ContentType.Application.Json)
setBody(tokenRequest) setBody(tokenRequest)
} }