Revert "Merge pull request #133 from dalbodeule/develop"

This reverts commit 83b5eaf34540d854382f0d0f00239529a78380a4, reversing
changes made to a99f3b342a73722d23e0b7328d08f68083b94b9e.
This commit is contained in:
dalbodeule 2025-05-27 13:18:52 +09:00
parent 83b5eaf345
commit 1c4b818a85
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
2 changed files with 4 additions and 59 deletions

View File

@ -11,7 +11,6 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory
import space.mori.chzzk_bot.chatbot.chzzk.Connector.getChannel
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.models.User
import space.mori.chzzk_bot.common.services.LiveStatusService
@ -222,13 +221,14 @@ class UserHandler(
throw RuntimeException("AccessToken or RefreshToken is not valid.")
}
try {
val tokens = Connector.client.refreshAccessToken(user.refreshToken!!)
client = Connector.getClient(tokens.first, tokens.second)
UserService.setRefreshToken(user, tokens.first, tokens.second)
client = Connector.getClient(user.accessToken!!, user.refreshToken!!)
client.loginAsync().join()
client.refreshTokenAsync().join()
UserService.setRefreshToken(user, client.loginResult.accessToken(), client.loginResult.refreshToken())
listener = ChzzkSessionBuilder(client).buildUserSession()
listener.createAndConnectAsync().join()

View File

@ -1,55 +0,0 @@
package space.mori.chzzk_bot.chatbot.utils
import com.google.gson.Gson
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import space.mori.chzzk_bot.common.utils.client
import xyz.r2turntrue.chzzk4j.ChzzkClient
import java.io.IOException
val client = OkHttpClient.Builder()
.addNetworkInterceptor { chain ->
chain.proceed(
chain.request()
.newBuilder()
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
.build()
)
}
.build()
val gson = Gson()
data class RefreshTokenResponse(
val accessToken: String,
val refreshToken: String,
val expiresIn: Int,
val tokenType: String = "Bearer",
val scope: String
)
fun ChzzkClient.refreshAccessToken(refreshToken: String): Pair<String, String> {
val url = "https://openapi.chzzk.naver.com/auth/v1/token"
val request = Request.Builder()
.url(url)
.header("Content-Type", "application/json")
.post(gson.toJson(mapOf(
"grantType" to "refresh_token",
"refreshToken" to refreshToken,
"clientId" to this.apiClientId,
"clientSecret" to this.apiSecret
)).toRequestBody())
.build()
client.newCall(request).execute().use { response ->
try {
if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}")
val body = response.body?.string()
val data = gson.fromJson(body, RefreshTokenResponse::class.java)
return Pair(data.accessToken, data.refreshToken)
} catch(e: Exception) {
throw e
}
}
}