All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.stormbit.vksdk.clients.VkUserClient.kt Maven / Gradle / Ivy

The newest version!
package com.github.stormbit.vksdk.clients

import com.github.stormbit.vksdk.longpoll.LongPoll
import com.github.stormbit.vksdk.objects.Captcha
import com.github.stormbit.vksdk.objects.TwoFactor
import com.github.stormbit.vksdk.objects.upload.Upload
import com.github.stormbit.vksdk.utils.VK_API_VERSION
import com.github.stormbit.vksdk.utils.append
import com.github.stormbit.vksdk.utils.getClientId
import com.github.stormbit.vksdk.vkapi.Auth
import com.github.stormbit.vksdk.vkapi.methods.account.AccountApi
import com.github.stormbit.vksdk.vkapi.methods.audio.AudioApi
import com.github.stormbit.vksdk.vkapi.methods.docs.DocsApi
import com.github.stormbit.vksdk.vkapi.methods.friends.FriendsApi
import com.github.stormbit.vksdk.vkapi.methods.groups.GroupsApi
import com.github.stormbit.vksdk.vkapi.methods.likes.LikesApi
import com.github.stormbit.vksdk.vkapi.methods.messages.MessagesApi
import com.github.stormbit.vksdk.vkapi.methods.photos.PhotosApi
import com.github.stormbit.vksdk.vkapi.methods.users.UsersApi
import com.github.stormbit.vksdk.vkapi.methods.video.VideoApi
import com.github.stormbit.vksdk.vkapi.methods.wall.WallApi
import io.ktor.client.*
import io.ktor.http.*

/**
 * Login via official VK app
 */
class VkUserClient(
    httpClient: HttpClient
) : LoginClient(httpClient) {

    companion object {
        internal const val BASE_PROXY_OAUTH_URL = "https://vk-oauth-proxy.xtrafrancyz.net/"
        internal const val CLIENT_ID = 2274003
        internal const val CLIENT_SECRET = "hHbZxrka2uZ6jB1inYsH"
        internal val HEADER = parametersOf("user-agent", "VKAndroidApp/5.40-3904")
        internal const val RECEIPT = "JSv5FBbXbY:APA91bF2K9B0eh61f2WaTZvm62GOHon3-vElmVq54ZOL5PHpFkIc85WQUxUH_wae8YEUKkEzLCcUC5V4bTWNNPbjTxgZRvQ-PLONDMZWo_6hwiqhlMM7gIZHM2K2KhvX-9oCcyD1ERw4"
    }

    override val longPoll: LongPoll = LongPoll(this)

    val audio = AudioApi(this)
    override val account: AccountApi = AccountApi(this)
    override val messages: MessagesApi = MessagesApi(this)
    override val groups: GroupsApi = GroupsApi(this)
    override val users: UsersApi = UsersApi(this)
    override val photos: PhotosApi = PhotosApi(this)
    override val video: VideoApi = VideoApi(this)
    override val docs: DocsApi = DocsApi(this)
    override val friends: FriendsApi = FriendsApi(this)
    override val likes: LikesApi = LikesApi(this)
    override val wall: WallApi = WallApi(this)
    override val upload: Upload = Upload(this)

    override suspend fun auth(
        login: String,
        password: String,
        captcha: Captcha?,
        twoFactor: TwoFactor?
    ): Auth.Response {
        val auth = Auth(login, password, httpClient)
        val authResponse = auth.authByVk(captcha)

        if (authResponse is Auth.Response.Success) {
            token = authResponse.token
            baseParams = Parameters.build {
                append("v", VK_API_VERSION)
                append("access_token", token)
            }
            headers = HEADER
            id = getClientId()

            log.info("Client object created with id $id")

            return authResponse
        }

        return authResponse
    }

    override suspend fun auth(token: String): VkUserClient {
        this.token = token
        baseParams = Parameters.build {
            append("v", VK_API_VERSION)
            append("access_token", token)
        }
        headers = HEADER
        id = getClientId()

        log.info("Client object created with id $id")

        return this
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy