我在我的android库上使用Retrofit进行Web请求。奇怪的是,如果我使用HTTPS的some
请求失败并且其他人通过了 - 并且失败消息是{“detail”:“未提供身份验证凭据。”}
似乎某种方式,当我为某些网络请求执行HTTPS时,我的authtoken没有被传递
05-09 13:47:10.110 6850-6890/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 200 http://staging.earthmiles.co.uk/api/v1/accounts/profile/firstupdate/2419/ (1187ms)
05-09 13:47:10.513 6850-6890/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 200 http://staging.earthmiles.co.uk/api/v1/globalprefs/connectedapps/ (213ms)
05-09 13:47:12.045 6850-6890/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 200 http://staging.earthmiles.co.uk/api/v1/rewards/rewards?page=1 (408ms)
05-09 13:47:12.354 6850-6962/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 200 http://staging.earthmiles.co.uk/api/v1/accounts/profile/me/ (686ms)
05-09 13:47:12.969 6850-6964/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 200 http://staging.earthmiles.co.uk/api/v1/feeds/feeds?page=1&segment_type=content (1279ms)
05-09 13:47:13.079 6850-6965/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 200 http://staging.earthmiles.co.uk/api/v1/feeds/feeds?page=1&segment_type=friends (1366ms)
05-09 13:47:13.405 6850-6890/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 200 http://staging.earthmiles.co.uk/api/v1/rewards/rewards?page=2 (1155ms)
05-09 13:48:03.584 7053-7094/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 200 https://staging.earthmiles.co.uk/api/v1/globalprefs/iosapppreferences/1/ (262ms)
05-09 13:48:10.731 7053-7094/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 200 https://staging.earthmiles.co.uk/api/v1/accounts/checkuserwithemail/?email=test295%40xyz.com (114ms)
05-09 13:48:12.528 7053-7094/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 201 https://staging.earthmiles.co.uk/api/v1/accounts/register/ (198ms)
05-09 13:48:13.375 7053-7094/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 200 https://staging.earthmiles.co.uk/api/v1/accounts/profile/firstupdate/2420/ (800ms)
05-09 13:48:13.801 7053-7094/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 200 https://staging.earthmiles.co.uk/api/v1/globalprefs/connectedapps/ (200ms)
05-09 13:48:15.578 7053-7168/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 200 https://staging.earthmiles.co.uk/api/v1/accounts/profile/me/ (349ms)
05-09 13:48:15.617 7053-7094/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 403 https://staging.earthmiles.co.uk/api/v1/rewards/rewards?page=1 (424ms)
05-09 13:48:15.692 7053-7169/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 403 https://staging.earthmiles.co.uk/api/v1/feeds/feeds?page=1&segment_type=content (442ms)
05-09 13:48:15.734 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 403 https://staging.earthmiles.co.uk/api/v1/feeds/feeds?page=1&segment_type=friends (449ms)
05-09 13:47:11.713 6850-6965/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ ---> HTTP GET http://staging.earthmiles.co.uk/api/v1/feeds/feeds?page=1&segment_type=friends
05-09 13:47:11.713 6850-6965/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Screen-Width: 1080
05-09 13:47:11.713 6850-6965/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Screen-Height: 1776
05-09 13:47:11.713 6850-6965/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Screen-Density: 3.0
05-09 13:47:11.713 6850-6965/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Screen-Scaled-Density: 3.0
05-09 13:47:11.713 6850-6965/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Screen-Density-DPI: 480
05-09 13:47:11.713 6850-6965/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Device-Type: android
05-09 13:47:11.713 6850-6965/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ App-Version: 1.0
05-09 13:47:11.713 6850-6965/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Authorization: Token 1f125a32123fe8c465bd16f30e1a21064c144c7b
05-09 13:47:11.713 6850-6965/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ ---> END HTTP (no body)
当我在Postmand中使用此authtoken时,我从服务器获得了有效的响应
05-09 13:48:15.734 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- HTTP 403 https://staging.earthmiles.co.uk/api/v1/feeds/feeds?page=1&segment_type=friends (449ms)
05-09 13:48:15.734 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Server: nginx/1.4.6 (Ubuntu)
05-09 13:48:15.734 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Date: Sat, 09 May 2015 17:48:44 GMT
05-09 13:48:15.734 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Content-Type: application/json
05-09 13:48:15.734 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Transfer-Encoding: chunked
05-09 13:48:15.734 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Connection: keep-alive
05-09 13:48:15.734 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Vary: Accept, Cookie
05-09 13:48:15.735 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ X-Frame-Options: SAMEORIGIN
05-09 13:48:15.735 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ Allow: GET, HEAD, OPTIONS
05-09 13:48:15.735 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ OkHttp-Selected-Protocol: http/1.1
05-09 13:48:15.735 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ OkHttp-Sent-Millis: 1431193695653
05-09 13:48:15.735 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ OkHttp-Received-Millis: 1431193695733
05-09 13:48:15.735 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ {"detail": "Authentication credentials were not provided."}
05-09 13:48:15.735 7053-7170/com.gmspartnersltd.earthmiles.debug D/Retrofit﹕ <--- END HTTP (59-byte body)
public class EMRestClient {
private static EMRestInterface mEMRestService;
public static EMRestInterface getmEMRestService() {
if (mEMRestService == null) {
Gson gson = new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class);
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
})
.registerTypeAdapter(Date.class, new DateSerializer())
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) // converts 'short_description' in JSON to shortDescription, java style
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(App.BASE_URL) // sets base url
.setRequestInterceptor(new AuthTokenHeaderInterceptor()) // provides Authorization : Token XYZ header if required
.setLogLevel(RestAdapter.LogLevel.FULL) // logs all input and output request/response for debug
.setConverter(new GsonConverter(gson)) // provides additional conversion functions from json if reqd
.build();
mEMRestService = restAdapter.create(EMRestInterface.class);
}
return mEMRestService;
}
}
public class AuthTokenHeaderInterceptor implements RequestInterceptor {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Screen-Width", String.valueOf(App.SCREEN_WIDTH));
request.addHeader("Screen-Height", String.valueOf(App.SCREEN_HEIGHT));
request.addHeader("Screen-Density", String.valueOf(App.SCREEN_DENSITY));
request.addHeader("Screen-Scaled-Density", String.valueOf(App.SCREEN_SCALED_DENSITY));
request.addHeader("Screen-Density-DPI", String.valueOf(App.SCREEN_DENSITY_DPI));
request.addHeader("Device-Type", App.DEVICE_TYPE);
request.addHeader("App-Version", App.APP_VERSION);
if (App.getLoginData()!=null) {
String authToken = App.getLoginData().getAuthToken();
if (!authToken.equals("")) {
String authTokenString = "Token " + authToken;
request.addHeader("Authorization", authTokenString);
}
}
}
}