简体   繁体   English

Spotify API - Kotlin 中的客户端凭证流与 Retrofit 不起作用

[英]Spotify API - Client Credentials Flow in Kotlin with Retrofit doesnt work

I am trying to use Spotify API.我正在尝试使用 Spotify API。 To use the API i need to get a new auth token.要使用 API,我需要获得一个新的身份验证令牌。 So per their guides here , I wrote the code below to request for a new token:因此,根据他们的指南我编写了以下代码来请求新令牌:

在此处输入图像描述

this is my API interface这是我的 API 接口

@FormUrlEncoded
    @POST("api/token")
    fun getToken(@Header("Authorization") auth:String,
                 @Header("Content-Type") content :String,
                 @Field(("grant_type")) grantType:String ): Call<Token>

this is my MainActivity:这是我的主要活动:

val retrofit: Retrofit = Retrofit.Builder().baseUrl(TOKEN_URL)
            .addConverterFactory(GsonConverterFactory.create()).build()
        val service :TopTrackApi = retrofit.create(TopTrackApi::class.java)

        val base64String = "Basic " +  get64BaseString("6bcfc9a9fccd45c8b9bb9c980f03e653:96c65e1824084adfb1ae99ef97734621")

        Log.i("64Base ", base64String)
        val listCall : Call<Token> = service.getToken(base64String,"application/x-www-form-urlencode","client_credentials")

        listCall.enqueue(object : Callback<Token>{
            override fun onResponse(response: Response<Token>?, retrofit: Retrofit?) {
                if (response?.body() != null) {
                Log.i("Response!", response.body().access_token)
                }
                if(response?.body() == null){
                    Log.i("Response!", "null response body")
                }
            }
            override fun onFailure(t: Throwable?) {
                Log.e("Error", t!!.message.toString())
            }
        })

fun get64BaseString(value:String):String{
        return Base64.encodeToString(value.toByteArray(),Base64.NO_WRAP)
    }

This is the token class as per their json object:这是根据他们的 json object 的令牌 class: 在此处输入图像描述

data class Token(
    val access_token: String,
    val expires_in: Int,
    val token_type: String
)

As a result I get this in Logcat:结果,我在 Logcat 中得到了这个:

I/Response: null response body

edit:When i am running the same values in a curl command it works(it returns the json object)编辑:当我在 curl 命令中运行相同的值时,它可以工作(它返回 json 对象)

What am I doing incorrect?我在做什么不正确? Thanks in advance.提前致谢。

I think your API interface should look like this:我认为您的 API 界面应该如下所示:

@Headers("Content-Type: application/x-www-form-urlencoded")
@POST("api/token")
    fun getToken(@Header("Authorization") auth:String,
                 @Field(("grant_type")) grantType:String ): Call<Token>
val base64String = "Basic "+ get64BaseString("6bcfc9a9fccd45c8b9bb9c980f03e653:96c65e1824084adfb1ae99ef97734621")

I think you are missing basic keyword.我认为您缺少基本关键字。 You need to add it as prefix to base 64 endcoded client id and secret.您需要将其作为前缀添加到 base 64 endcoded client id 和 secret。 There are several authorization types in HTTP for Authorization Header. HTTP中有几种授权类型,用于授权Header。 Since spotify uses Basic Auth, you need to specify it.由于 spotify 使用基本身份验证,因此您需要指定它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM