for the past few weeks ive been trying to implement spotify webapi on my app to auth users, get their pfp and handler and also get their currently playing track name and artist name but had no success. id appreciate any help.
AuthViewModel:
package com.example.grooveshare.viewmodels import android.content.Context import android.content.Intent import android.net.Uri import android.util.Log import androidx.activity.ComponentActivity import androidx.activity.result.ActivityResultLauncher import androidx.lifecycle.ViewModel import net.openid.appauth.* import java.security.MessageDigest 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() } } }
the logs i get everytime the app runs are the following:
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
steps ive taken so far:
made sure my redirect uri stated in the viewmodel is the same w the one stated in spotify dashboard and also that the intent is correctly established in AndroidManifest.xml