改造中添加标题授权的问题

我的授权有问题。我有令牌。现在,我正在尝试使用拦截器添加带有令牌的标头。

我的代码:

 val authInterceptor = object : Interceptor {
    override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
        val original = chain.request()
        val requestBuilder = original.newBuilder()
        requestBuilder.addHeader("Authorization", "Bearer " +  ACCESS_TOKEN)
        return chain.proceed(requestBuilder.build())
    }
}

val okHttpClient =  OkHttpClient.Builder()
    .addInterceptor(authInterceptor)
    .build()

val retrofit = Retrofit.Builder()
    .addConverterFactory(GsonConverterFactory.create())
    .client(okHttpClient)
    .baseUrl(BASE_URL)
    .build()}

val service: CategoryServiceInterface = retrofit.create(CategoryServiceInterface::class.java)

val call: Call<Category> =  service.getCategory( CLIENT_ID,CLIENT_SECRET )
val callback = object : Callback<Category> {
    override fun onFailure(call: Call<Category>, t: Throwable) {
        println("Throwable ${t.message}")
    }

    override fun onResponse(call: Call<Category>, response: Response<Category>) {
        println("response ${response}")
    }

}
service.getCategory( CLIENT_ID,CLIENT_SECRET).enqueue(callback)


interface CategoryServiceInterface {
    @GET("/v3/categories")
    fun getCategory(
        @Query("client_id") client_id: String,
        @Query("client_secret") client_secret: String
    ): Call<Category>
}

我从服务器收到错误

信息:{“ errorCode”:401,“ errorId”:“ ap14int-sv2.2020040811.3034789”,“ errorMessage”:“必须提供授权令牌”}

Change: requestBuilder.addHeader("Authorization", "Bearer " + ACCESS_TOKEN) to requestBuilder.addHeader("Authorization", ACCESS_TOKEN)

没有帮助 我也尝试过这种方式

@GET("/v3/categories")
fun getCategory(
    @Query("client_id") client_id: String,
    @Query("client_secret") client_secret: String,
    @Header("Authorization") access_token: String
): Call<Category>
...
service.getCategory( CLIENT_ID,CLIENT_SECRET,ACCESS_TOKEN ).enqueue(callback)

没有结果 当我在Postman中生成代码时:

OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder()
  .url("https://sandbox7.feedly.com/v3/categories?client_id=xxx&client_secret=xxxx")
  .method("GET", null)
  .addHeader("Authorization", "xxxx")
  .build();
Response response = client.newCall(request).execute();

它正在工作。 错误在哪里?

评论