Type in your question below and we'll check to see what answers we can find...
Loading article...
Submitting...
If you couldn't find any answers in the previous step then we need to post your question in the community and wait for someone to respond. You'll be notified when that happens.
Simply add some detail to your question and refine the title if needed, choose the relevant category, then post.
Before we can post your question we need you to quickly make an account (or sign in if you already have one).
Don't worry - it's quick and painless! Just click below, and once you're logged in we'll bring you right back here and post your question. We'll remember what you've already typed in so you won't have to do it again.
Please see below the most popular frequently asked questions.
Loading article...
Loading faqs...
Please see below the current ongoing issues which are under investigation.
Loading issue...
Loading ongoing issues...
Plan
Premium
Country
Spain
My suggestion was marked as spam as soon as I posted it.
I'm really trying to implement the API and I have a real problem like a comment in the post, but as soon I posted it in the forum, it was markes as spam.
I hope this can be unmarked as spam and the time stamp put back as a new post so people can see.
The url to the question is:
I can't see that post, but "invalid client" can be caused by two things (assuming the redirect uri is correct):
- Trying to use Client Credentials when Authorization is required.
- Not using the the required Authorization Scope(s).
These points are well configured because I have tried with the same data to make a request from an API Tester and it returns a valid response, but not from the app.
I show it in the post, it's mainly in case someone else had happened to him implementing the library in kotlin.
I can't see the post. Can you please give more details?
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]
I've found this four year old tutorial, and hope it still works: https://tolkiana.com/how-to-use-spotifys-sdk-in-kotlin/
Note: I don't see the use of scopes in your code, but that shouldn't be the problem with getting a code.
Hey there you, ย Yeah, you!ย 😁 ย Welcome - we're glad you joined the Spotify Community! ย While you here, let's have a fun game and getโฆ