jeasonlzy / okhttp-OkGo

OkGo - 3.0 震撼来袭,该库是基于 Http 协议,封装了 OkHttp 的网络请求框架,比 Retrofit 更简单易用,支持 RxJava,RxJava2,支持自定义缓存,支持批量断点下载管理和批量上传管理功能
Apache License 2.0
10.6k stars 2.55k forks source link

Authenticator失败 #418

Open wuMaoTou opened 7 years ago

wuMaoTou commented 7 years ago
    String basic = Credentials.basic("username", "password");
     HttpHeaders headers = new HttpHeaders();
     headers.put("Authorization", basic);
        OkGo.post(url)
                .tag(this)
                .headers(headers)
                .execute(new DialogCallback<Model<Object>>(getActivity()) {
                    @Override
                    public void onSuccess(Model<Object> voidModel, Call call, Response response) {
                        if (getView() != null) {
                            getView().getToken();
                        }
                    }
                    @Override
                    public void onFailure(Call call, int errorCode, String errorMsg) {
                        super.onFailure(call, errorCode, errorMsg);
                        if (getView() != null) {
                            getView().showErrorMsg(errorMsg);
                        }
                    }
                });
--> POST [url] http/1.1
    Accept-Language: zh-CN%2Czh%3Bq%3D0.8
    User-Agent: Mozilla%2F5.0+%28Linux%3B+U%3B+Android+4.4.4%3B+zh-cn%3B+genymotion_vbox86p_4.4.4_170320_221603+Build%2FKTU84P%29+AppleWebKit%2F534.30+%28KHTML%2C+like+Gecko%29+Version%2F4.0+Mobile+Safari%2F534.30
    Authorization: Basic+d2ViOmI3ZDc3YjY5NDczMGU4ZDVkMTc2ZWJjYTE1MDBhODU
    Content-Length: 0

    body:
 --> END POST
 <-- 401 [url](10ms)
    WWW-Authenticate: Basic realm="oauth2/client"
    X-Content-Type-Options: nosniff
    X-XSS-Protection: 1; mode=block
    Cache-Control: no-cache, no-store, max-age=0, must-revalidate
    Pragma: no-cache
    Expires: 0
    X-Frame-Options: DENY
    Content-Type: application/json;charset=UTF-8
    Transfer-Encoding: chunked
    Date: Mon, 21 Aug 2017 10:34:05 GMT

    body:{"timestamp":1503311645525,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":""}
 <-- END HTTP

Authenticator授权失败,用okhttpclient发起请求是成功的,是否有其他方法?烦请赐教

已下代码是请求成功的:

        String basic = Credentials.basic("username", "password");
        Request request = new Request.Builder()
                .post(RequestBody.create(HttpParams.MEDIA_TYPE_JSON,""))
                .url(url)
                .header("Authorization", basic)
                .build();
        OkGo.getInstance().getOkHttpClient().newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if(getView()!=null)
                    getView().showErrorMsg(getActivity().getString(R.string.activity_login_fail));
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    JsonReader jsonReader = new JsonReader(response.body().charStream());
                    Model baseModel = new Gson().fromJson(jsonReader, Model.class);
                    response.close();
                }
            }
        });
tank21 commented 6 years ago

你好,请问你解决这个问题没呢,我也遇到了

hanxueodngdong commented 4 years ago

您好 解决了吗?我也遇到了