|
|
@@ -0,0 +1,89 @@
|
|
|
+package com.adealink.weparty.account.login.auth
|
|
|
+
|
|
|
+import android.app.Activity
|
|
|
+import android.content.Intent
|
|
|
+import com.adealink.frame.log.Log
|
|
|
+import com.adealink.weparty.BuildConfig
|
|
|
+import com.adealink.weparty.account.constant.TAG_TIKTOK_AUTH
|
|
|
+import com.adealink.weparty.account.login.util.PKCEUtils
|
|
|
+import com.tiktok.open.sdk.auth.AuthApi
|
|
|
+import com.tiktok.open.sdk.auth.AuthRequest
|
|
|
+import java.lang.ref.WeakReference
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by XiaoDongLin.
|
|
|
+ * Date: 2025/2/22
|
|
|
+ */
|
|
|
+class TiktokAuth(
|
|
|
+ override val activityRef: WeakReference<Activity>?,
|
|
|
+ override val authCallback: IAuthCallback?
|
|
|
+) : IAuth {
|
|
|
+
|
|
|
+ override var secret: String? = ""
|
|
|
+ private var authApi: AuthApi? = null
|
|
|
+
|
|
|
+ override fun auth() {
|
|
|
+ val activity = activityRef?.get() ?: return
|
|
|
+ Log.i(TAG_TIKTOK_AUTH, "auth start")
|
|
|
+
|
|
|
+ if (authApi == null) {
|
|
|
+ authApi = AuthApi(activity)
|
|
|
+ }
|
|
|
+ val authRequest = AuthRequest(
|
|
|
+ clientKey = BuildConfig.TIKTOK_CLIENT_KEY,
|
|
|
+ scope = "user.info.basic",
|
|
|
+ redirectUri = BuildConfig.TIKTOK_REDIRECT_URL,
|
|
|
+ codeVerifier = PKCEUtils.generateCodeVerifier()
|
|
|
+ )
|
|
|
+ authApi?.authorize(
|
|
|
+ request = authRequest,
|
|
|
+ authMethod = AuthApi.AuthMethod.TikTokApp
|
|
|
+ )
|
|
|
+
|
|
|
+ handleAuthResponse(activity.intent)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onNewIntent(intent: Intent?) {
|
|
|
+ handleAuthResponse(intent)
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun handleAuthResponse(intent: Intent?) {
|
|
|
+ intent ?: return
|
|
|
+ val activity = activityRef?.get() ?: return
|
|
|
+ val authApi = authApi ?: return
|
|
|
+
|
|
|
+ val authResponse = authApi.getAuthResponseFromIntent(
|
|
|
+ intent = activity.intent,
|
|
|
+ redirectUrl = ""
|
|
|
+ ) ?: return
|
|
|
+
|
|
|
+ Log.i(TAG_TIKTOK_AUTH, "handleAuthResponse authResponse:$authResponse")
|
|
|
+
|
|
|
+ val authCode = authResponse.authCode
|
|
|
+ val grantedPermissions = authResponse.grantedPermissions // Granted scopes
|
|
|
+
|
|
|
+ // Please refer to the Handling Errors for more detail.
|
|
|
+ val authError = authResponse.authError
|
|
|
+ val authErrorDescription = authResponse.authErrorDescription
|
|
|
+
|
|
|
+// state 用于防止 CSRF(跨站请求伪造) 攻击,TikTok 授权后会返回 state,你可以在回调时进行验证:
|
|
|
+
|
|
|
+ if (authCode.isNotEmpty()) {
|
|
|
+ //todo:授权成功
|
|
|
+ } else if (authError?.isNotEmpty() == true || authErrorDescription?.isNotEmpty() == true) {
|
|
|
+ //todo:授权失败
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun logout() {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|