“技术内容”之前的简报
对于使用 Retrofit 并不陌生,但遇到了这种奇怪的行为,我很难理解和修复它,
我有两个网络服务,在 Postman 和 iOS 中都按预期工作正常,但只有一个在 Retrofit 中工作,而另一个则不行,
在我的辩护中,我可以说我收到了(未经授权的)响应,这意味着我能够访问服务器并获得结果
在 API 开发人员的辩护中,他说它可以在 Postman 和其他设备上工作,所以不是服务问题
如果有任何改造专家告诉我改造可能在我背后做了什么才能得到这个错误?
技术资料
说到服务类型,它包含授权承载令牌作为标头,每 6 小时过期一次,并且根本不包含任何参数(所以它应该很简单,对吧?)和一个简单的 urlhttp://hashchuna.nn-assets.com/api/locations
不幸的是,标头令牌无法与有效密钥共享,因为它在任何人都可以尝试之前就已过期,但无论如何授权持有者 3d44626a55dbb024725984e0d37868336fd7e48a
我尝试过的
我正在使用 okhttp 拦截来添加授权标头以使用两者进行请求添加标题/标题方法,url 中没有空格,因为没有参数
改造时出现 401 未经授权的错误?
Java:Android:改造 - 使用 Call but,Response{code = 401,message=unauthorized}
https://github.com/square/retrofit/issues/1290
但他们都没有帮忙
WARNING
现在要记住的棘手部分是,令牌过期时必须给出 401 错误,这是预期的,但问题是即使对于新创建的令牌我也会得到 401 ,这是我的核心问题
LOG
D/OkHttp: --> GET http://hashchuna.nn-assets.com/api/locations http/1.1
D/OkHttp: Authorization: Bearer 7c0d53de006b6de931f7d8747b22442354cecef9
D/OkHttp: --> END GET
D/OkHttp: <-- 401 Unauthorized http://hashchuna.nn-assets.com/api/locations (773ms)
D/OkHttp: Date: Mon, 20 Feb 2017 10:44:11 GMT
D/OkHttp: Server: Apache
D/OkHttp: X-Powered-By: PHP/7.0.15
D/OkHttp: Access-Control-Allow-Origin: *
D/OkHttp: Access-Control-Allow-Credentials: true
D/OkHttp: Access-Control-Max-Age: 1000
D/OkHttp: Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding
D/OkHttp: Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
D/OkHttp: Expires: Thu, 19 Nov 1981 08:52:00 GMT
D/OkHttp: Cache-Control: no-store, no-cache, must-revalidate
D/OkHttp: Pragma: no-cache
D/OkHttp: Set-Cookie: PHPSESSID=u477o8g0q387t92hms4nhc14n1; path=/
D/OkHttp: Vary: Authorization
D/OkHttp: X-Powered-By: PleskLin
D/OkHttp: Keep-Alive: timeout=5
D/OkHttp: Connection: Keep-Alive
D/OkHttp: Transfer-Encoding: chunked
D/OkHttp: Content-Type: application/json;charset=utf-8
D/OkHttp: <-- END HTTP
CODE
截距
Request request = chain
.request()
.newBuilder()
//.header("Authorization","Bearer "+ SharedPrefsUtils.getSPinstance().getAccessToken(context))
.addHeader("Authorization","Bearer 1ed6b7c1839e02bbf7a1b4a8dbca84d23127c68e")
//.addHeader("cache-control", "no-cache")
//.cacheControl(CacheControl.FORCE_NETWORK)
.build();
改造实例
private Api getApiInstance(Context context) {
HttpLoggingInterceptor logInter = new HttpLoggingInterceptor();
logInter.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient mIntercepter = new OkHttpClient.Builder()
.addInterceptor(new RequestResponseInterseptor(context))
.addInterceptor(logInter)
.build();
Retrofit retrofitInstance = new Retrofit.Builder()
//.addConverterFactory(new NullOnEmptyConverterFactory())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.client(mIntercepter)
.build();
return retrofitInstance.create(Api.class);
}