Ujj*_*jju 7 android ios retrofit okhttp postman
简介之前'技术性的东西'
对于使用Retrofit并不熟悉,但遇到了这种奇怪的行为,我很难理解和修复,我有两个网络服务,都在Postman和iOS中正常工作,但只有一个在改造而不是其他,
在我的辩护中,我可以说我得到(未经授权)响应,这意味着我能够打到服务器并获得结果
在API开发人员的辩护中,他说它适用于Postman和其他设备,所以不是服务问题
如果有任何Retrofit专家告诉我为了得到这个错误,我背后可能会做什么改造?
技术小说在
讨论服务类型时,它包含授权承载令牌作为标题,每6个小时到期并且根本不包含参数(因此它应该很简单,对吧?)和一个简单的URL
http://hashchuna.nn-assets .com/api/locations
不幸的是,标头令牌无法与有效密钥共享,因为它在任何人都可以尝试之前就会过期,但无论如何它都是授权承载3d44626a55dbb024725984e0d37868336fd7e48a
我做了什么我
正在使用okhttp拦截添加授权标头来请求使用addHeader/header方法,url中没有空格cos没有参数
在改造中获得401未经授权的错误?
Java:Android:Retrofit - 使用Call但是,Response {code = 401,message = unauthorized}
https://github.com/square/retrofit/issues/1290
但是他们没有帮助
警告
现在要记住一个棘手的部分,令牌过期时必须给出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
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)
改造实例
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);
}
Run Code Online (Sandbox Code Playgroud)
解决方案(它的COOKIE)
由于一些技巧,服务不兼容的实际原因是,假设POSTMAN和iOS客户端存储并COOKIE在需要时自行重用,无需任何显式处理,Postman中的Cookie可以进行测试使用Postman Intercepter,但无法编辑,因为chrome不允许通过插件编辑cookie
但是改造/ OkHttp除非另有说明会考虑禁用(出于安全原因可能),
饼干添加任何内部Interseptor的消息头的一个addHeader("Cookie","KEY-VALUE")
或
使用cookieJar添加到
OkHttpClient mIntercepter = new OkHttpClient.Builder()
.cookieJar(mCookieJar)
.addInterceptor(new RequestResponseInterseptor(context))
.addInterceptor(logInter)
.build();
Run Code Online (Sandbox Code Playgroud)
根据您的需要和cookie类型
我认为您正在Retrofit为您添加其他标头,导致您的API不在乎Authorization标头。下面的代码会将标头添加到您现有的标头中,而不是覆盖它们。
OkHttpClient mIntercepter = new OkHttpClient.Builder()
...
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("Authorization", "Bearer " + "1ed6b7c1839e02bbf7a1b4a8dbca84d23127c68e").build();
return chain.proceed(request);
})
...
.build();
Run Code Online (Sandbox Code Playgroud)
这些标头的格式在这里是正确的,键应该是Authorization,值应该是Bearer 1ed6b7c1839e02bbf7a1b4a8dbca84d23127c68e(根据您的情况)。
| 归档时间: |
|
| 查看次数: |
9258 次 |
| 最近记录: |