gpt4 book ai didi

android - Webservice 在 Android Retrofit 中不工作,但在 Postman 和 Swift/iOS 中工作,获得 401 Unauthorized

转载 作者:可可西里 更新时间:2023-11-01 03:37:42 25 4
gpt4 key购买 nike

“技术内容”之前的简报
对使用 Retrofit 并不陌生,但遇到了这种我很难理解和修复的奇怪行为,我有两个 Web 服务,在 Postman 和 iOS 中都可以正常工作,但只有一个在 Retrofit 中有效,而另一个则不行,
在我的辩护中,我可以说我得到了(未经授权的)响应,这意味着我能够访问服务器并获得结果
在 API 开发人员的辩护中,他说它适用于 Postman 和其他设备,所以不是服务问题

如果有 Retrofit 专家告诉我 retrofit 可能在背后做了什么以得到这个错误?

技术资料
谈到服务类型,它包含 Authorization Bearer token 作为 header ,每 6 小时过期一次并且根本不包含任何参数(所以应该很简单,对吧?)和一个简单的网址 http://hashchuna.nn-assets.com/api/locations
不幸的是, header token 无法与有效 key 共享,因为它在任何人尝试之前就已过期,但无论如何它都在这里Authorization Bearer 3d44626a55dbb024725984e0d37868336fd7e48a

我尝试了什么
我正在使用 okhttp 拦截将授权 header 添加到使用 addHeader/header 方法的请求,url 中没有空格因为没有参数
Getting 401 unauthorized error in retrofit?
Java: Android: Retrofit - using Call but, Response{code = 401,message=unauthorized}
https://github.com/square/retrofit/issues/1290
但他们都没有帮助

警告
现在要记住棘手的部分,过期的 token 必须给出 401 错误,这是预期的,但问题是即使是新创建的 token 我也得到 401 ,这是我的核心问题

日志

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

代码
拦截

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);
}

最佳答案

解决方案(它的 COOKIE)
多亏了一些提示,服务不兼容的实际原因是,据推测 POSTMAN 和 iOS 客户端在发出请求时自行存储和重用 COOKIE 而无需显式处理,Postman 中的 Cookie 可以使用 Postman Intercepter 进行测试, 但无法编辑,因为 chrome 不允许通过插件编辑 cookie

但是 Retrofit/OkHttp 除非指定,否则将认为它已禁用(可能出于安全原因),
Cookie 在 Interseptor 中作为 header 之一添加 addHeader("Cookie","KEY-VALUE")
或者
使用cookieJar添加到

OkHttpClient mIntercepter = new OkHttpClient.Builder()
.cookieJar(mCookieJar)
.addInterceptor(new RequestResponseInterseptor(context))
.addInterceptor(logInter)
.build();

根据您的需要和 cookie 类型

关于android - Webservice 在 Android Retrofit 中不工作,但在 Postman 和 Swift/iOS 中工作,获得 401 Unauthorized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42375566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com