android - Android Retrofit OkHttpClient 拦截器添加标头获取错误“HTTP 403 Forbidden”
问题描述
所以,我的目标是将它嵌入api key
到我的Retrofit
对象中,这样我就不需要在每个请求函数中手动将它作为查询参数附加,然后我做了以下事情(学习:https ://proandroiddev.com/headers-in -改造-a8d71ede2f3e):
private val interceptor = Interceptor { chain ->
val newRequest = chain.request().newBuilder().run {
addHeader("api_key", Constants.API_KEY)
build()
}
chain.proceed(newRequest)
}
private val okHttpClient = OkHttpClient.Builder().run {
connectTimeout(15, TimeUnit.SECONDS)
readTimeout(15, TimeUnit.SECONDS)
addInterceptor(interceptor) //<- apply Interceptor
build()
}
//apply the okHttpClient to my Retrofit object...
但它失败并给出了这个错误:HTTP 403 Forbidden
.
PS:在添加之前Interceptor
一切正常
前:
@GET("neo/rest/v1/feed")
suspend fun getAsteroidsResult(
@Query("start_date") startDate: String,
@Query("end_date") endDate: String,
@Query("api_key") apiKey: String = Constants.API_KEY
): ResponseBody
解决方案
您能否添加日志拦截器并设置日志级别并提供日志?
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
像这样的东西:
OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
okBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC).setLevel
(HttpLoggingInterceptor.Level.BODY).setLevel(HttpLoggingInterceptor.Level.HEADERS))
推荐阅读
- airflow-scheduler - 安排气流间隔
- javascript - 如何阻止 Grommet 组件在 Safari 中循环
- java - 使用 ffmpeg 导出 .ogg 音频数据包
- git - 如何从克隆中排除子模块以避免用户无权访问时出错
- amazon-web-services - 使用 ECS 中的服务相关角色授予对另一个账户中 AWS 资源的访问权限
- azure - 如何在不使用 powershell 挂载的情况下查找 Azure 文件共享中存在的文件
- c# - 无法从数据库中检索字节数组图像并在图片框控件中显示
- python - 保存 perdition 模型并将新数据集插入其中
- mongodb - $accumulator(聚合)错误 mongo db
- typescript - 在 Typescript 中的函数中添加类型