Announcements

Help Wizard

Step 1

NEXT STEP

Getting {"error":"invalid_client"} in /api/token request

Getting {"error":"invalid_client"} in /api/token request

Plan

Premium

Country

Spain

 

I have the next code:

My retrofit client:

private const val TOKEN_URL: String = "https://accounts.spotify.com"

val TokenModule: Module
get() = module {
factory { TokenInterceptor(get()) }
factory {
OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.addNetworkInterceptor(get<TokenInterceptor>())
.build()
}
single(named("TokenRetrofit")) {
Retrofit.Builder()
.baseUrl(TOKEN_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(get())
.build()
}
}

With my interceptor:

companion object {
private const val TOKEN_HEADER_CHARSET_KEY = "Charset"
private const val TOKEN_HEADER_CHARSET_VALUE = "UTF-8"

private const val TOKEN_HEADER_CONTENT_TYPE_KEY = "Content-Type"
private const val TOKEN_HEADER_CONTENT_TYPE_VALUE = "application/x-www-form-urlencoded"

private const val TOKEN_HEADER_AUTH_KEY = "Authorization"
private const val TOKEN_HEADER_AUTH_VALUE = "Basic"
}

override fun intercept(chain: Interceptor.Chain): Response {
val authConfig = getAuthConfig()
val base64client = Base64.encodeToString("${authConfig.clientId}:${authConfig.campaign}".toByteArray(Charsets.UTF_8), Base64.URL_SAFE)

val original = chain.request()
val requestBuilder = original.newBuilder()
.addHeader(TOKEN_HEADER_CHARSET_KEY, TOKEN_HEADER_CHARSET_VALUE)
.addHeader(TOKEN_HEADER_CONTENT_TYPE_KEY, TOKEN_HEADER_CONTENT_TYPE_VALUE)
.addHeader(TOKEN_HEADER_AUTH_KEY, "$TOKEN_HEADER_AUTH_VALUE $base64client")
.url(original.url)
val request = requestBuilder.build()
return chain.proceed(request)
}

 With de TokenService implementing the request:

@FormUrlEncoded
@POST("api/token")
suspend fun getAccessToken(
@Field("code") code: String,
@Field("redirect_uri") redirectUri: String,
@Field("grant_type") grantType: String
): Call<TokenResponse>
override suspend fun invoke(code:String): TokenResponse? {
val auth = getAuthConfig()

val call = tokenService.getAccessToken(
grantType = "authorization_code",
code = code,
redirectUri = auth.redirectUrl
)
return try {
val response = call.execute()
if (response.isSuccessful) {
response.body()
} else {
null
}
} catch (e: IOException) {
null
}
}

 I use the same data in the /authorize request and it gives me the code that a pass by params in this last code, I also try this request in an Api tester and it gives me the access token, but in the IDE I'm making the request like that, but it gives me the next error in the logcat:

 

I/okhttp.OkHttpClient: --> POST https://accounts.spotify.com/api/token
I/okhttp.OkHttpClient: Content-Type: application/x-www-form-urlencoded
I/okhttp.OkHttpClient: Content-Length: 309
I/okhttp.OkHttpClient: code=AQCQUXqL-Drf8tHrNQ-jyb2Cv0ZUirTeuF6WJ7s27-ec7IdDVYard1gka2ECiBCp8-xXn2lV4hHtVjfRu2cCxGhSU10_4333t9aeg0F7U4G6Q9JZ20qlWsEn-ATX1BoZLzULP80K-CdVXcuVS5GZQljnljsWTM4GbgIIBpysTw9iCP3iUha5kksciJZf0Aflxm6OgIULp4QLGUR2NjwLH8le-vqBNRw6ixzNucyZ5lDcYQ&redirect_uri=spotiwrap%3A%2F%2Fauth&grant_type=authorization_code
I/okhttp.OkHttpClient: --> END POST (309-byte body)
D/TrafficStats: tagSocket(99) with statsTag=0xffffffff, statsUid=-1
I/okhttp.OkHttpClient: <-- 400 https://accounts.spotify.com/api/token (180ms)
I/okhttp.OkHttpClient: date: Tue, 18 Apr 2023 11:02:14 GMT
I/okhttp.OkHttpClient: content-type: application/json
I/okhttp.OkHttpClient: content-length: 26
I/okhttp.OkHttpClient: set-cookie: __Host-device_id=AQArQ8ol6yrP_I68g5dUQ39P-qQQRJrHEKPYRnTuIA_DQPYnYM65cZm6kSJ9fix5ANgCY7_r00fQsYM6tOWUFB6tPmeqF6-nxrw;Version=1;Path=/;Max-Age=2147483647;Secure;HttpOnly;SameSite=Lax
I/okhttp.OkHttpClient: {"error":"invalid_client"}
I/okhttp.OkHttpClient: <-- END HTTP (26-byte body)
E/AndroidRuntime: FATAL EXCEPTION: BackgroundDispatcher-worker-3
Process: com.wachon.spotiwrap, PID: 5316
retrofit2.HttpException: HTTP 400
at retrofit2.KotlinExtensions$await$2$2.onResponse(KotlinExtensions.kt:53)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:161)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
at java.lang.Thread.run(Thread.java:1012)
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@77da765, BackgroundDispatcher@69f3c3a]

 

BaltaJmn_0-1681816283367.png

 

Reply
0 Replies

Suggested posts