Spotify WebAPI安卓集成求助:认证回调Intent为空
安卓集成Spotify WebAPI认证回调Intent为空问题
近几周我尝试在安卓应用中集成Spotify WebAPI,目标实现用户认证、获取用户头像(pfp)、用户名(handler)以及当前播放曲目名称和艺术家名称,但始终未成功。目前核心问题是认证回调时Intent为空,以下是我的相关代码、运行日志及已做的排查步骤:
AuthViewModel代码
import java.security.SecureRandom import java.util.Base64 class SpotifyAuthViewModel : ViewModel() { private val clientId = "" private val redirectUri = "myapp://callback" private val authorizationEndpoint = "https://accounts.spotify.com/authorize" private val tokenEndpoint = "https://accounts.spotify.com/api/token" private val codeVerifier = generateCodeVerifier() private lateinit var authService: AuthorizationService fun initializeAuthService(context: Context) { authService = AuthorizationService(context) Log.d("SpotifyAuth", "AuthorizationService initialized") } private fun generateCodeVerifier(): String { val secureRandom = SecureRandom() val codeVerifier = ByteArray(32) secureRandom.nextBytes(codeVerifier) return Base64.getUrlEncoder().withoutPadding().encodeToString(codeVerifier) } private fun generateCodeChallenge(verifier: String): String { val bytes = verifier.toByteArray(Charsets.US_ASCII) val messageDigest = MessageDigest.getInstance("SHA-256") val digest = messageDigest.digest(bytes) return Base64.getUrlEncoder().withoutPadding().encodeToString(digest) } fun authenticate(authResultLauncher: ActivityResultLauncher<Intent>) { if (!::authService.isInitialized) { Log.e("SpotifyAuth", "AuthorizationService is not initialized") return } val serviceConfig = AuthorizationServiceConfiguration( Uri.parse(authorizationEndpoint), Uri.parse(tokenEndpoint) ) val authRequest = AuthorizationRequest.Builder( serviceConfig, clientId, ResponseTypeValues.CODE, Uri.parse(redirectUri) ).setCodeVerifier(codeVerifier, generateCodeChallenge(codeVerifier), "S256") .setScope("user-read-currently-playing") .build() Log.d("SpotifyAuth", "Authorization Request URI: ${authRequest.toUri()}") val authIntent = authService.getAuthorizationRequestIntent(authRequest) authResultLauncher.launch(authIntent) // Launch the auth intent using the Activity Result API } fun handleAuthResponse(intent: Intent?) { if (intent == null) { Log.e("SpotifyAuth", "Intent is null") return } Log.d("SpotifyAuth", "Handling Intent: $intent") Log.d("SpotifyAuth", "Intent Data: ${intent.data}") Log.d("SpotifyAuth", "Intent Extras: ${intent.extras}") val authorizationResponse = AuthorizationResponse.fromIntent(intent) val authorizationException = AuthorizationException.fromIntent(intent) if (authorizationResponse != null) { Log.d("SpotifyAuth", "Authorization Response: ${authorizationResponse.jsonSerializeString()}") val tokenRequest = authorizationResponse.createTokenExchangeRequest() authService.performTokenRequest(tokenRequest) { tokenResponse, exception -> if (tokenResponse != null) { Log.d("SpotifyAuth", "Token Response: ${tokenResponse.jsonSerializeString()}") val accessToken = tokenResponse.accessToken Log.d("SpotifyAuth", "Access Token: $accessToken") } else { Log.e("SpotifyAuth", "Token Exchange Failed: ${exception?.errorDescription}") } } } else { Log.e("SpotifyAuth", "Authorization Failed: ${authorizationException?.errorDescription}") } } override fun onCleared() { super.onCleared() if (::authService.isInitialized) { authService.dispose() } } }
运行日志
17:13:05.657 8857-8857 SpotifyAuth com.example.grooveshare D AuthorizationService initialized 17:13:05.662 8857-8857 SpotifyAuth com.example.grooveshare D Authorization Request URI: https://accounts.spotify.com/authorize?redirect_uri=myapp%3A%2F%2Fcallback&client_id=7ac6f34f4fd947d8abee1dbbcbc478f5&response_type=code&state=Yp97iSWgmoqqbVBzqzE3Dw&nonce=z1bk_2FURFxkkBHUtn-G2Q&scope=user-read-currently-playing&code_challenge=4IFKhxrW5p_PMYYnxoJ2KAvVbvErr4I4ZXtb8Sgy8SM&code_challenge_method=S256 17:13:11.010 8857-8857 SpotifyAuth com.example.grooveshare E Intent is null
已执行的排查步骤
- 确认ViewModel中的redirect uri与Spotify开发者后台配置完全一致
- 已在AndroidManifest.xml中正确配置Intent Filter
内容的提问来源于stack exchange,提问作者dimitris.terzz
相关产品推荐
相关产品推荐

