Web服务在Android Retrofit 2中给出错误401,但在Volley和Postman中有效

时间:2019-01-16 20:26:43

标签: android kotlin authorization retrofit2

一段时间以来,我一直在使用改造和身份验证,没有问题。 但是,通过此API调用,尽管一切都与我可以正常工作的其他项目完全相同,但我不断收到错误代码401

这是我的API调用代码:

@GET("workers")
    fun getWorkersList(): Observable<BaseResponseModel<WorkerAttributes>>

    companion object Factory {
        fun create(): WebServices {
            val okHttpClientBuilder = OkHttpClient.Builder()
            val httpLoggingInterceptor = HttpLoggingInterceptor()
            httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
            okHttpClientBuilder.addInterceptor(httpLoggingInterceptor)
            okHttpClientBuilder.addInterceptor { chain ->
                val request = chain.request().newBuilder()
                request.addHeader("Authorization", "Bearer eyJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiUlNBLU9BRVAiLCJraWQiOiIzaGltWWEyYmJHS18zSVdZMUxUUHdyTUl2ZkF3ZVhZQmRNLTU5XzN6c0MwIn0.DIwk0Q215_M7rt3iGfyRVrImhKCXvEB5NBoLQj6iqBnN9yCWWBhGW9fmX6beOajtWgC8gpupe1hJ-iurFRbo2Eg52Nh9ljkkO5Mz8ulSpH7YlfP8Yi-TBrNoPIf_IvHrr1Rt8bG0w2Ie1Jo3BBOguMQ6Q2WI4fQDY-oFIluxOQ5t9E0_XvDhna7maiQnWevLbu80Mj7g643v5nvNWOdpSAXW91rZXIXdal_A_ffY-IUyvYhEJuOkyHWwNonuGjiGHWKPTjTR16w3seoSs4srkacOIN1cmMdXKlkvlWSYCv5Nd8xJ_rTqA4GIyKLRSALYcuiTFMOIxeMIpPB5DjpMTOmOD72QnD8MHMx-SNHk2Yva5iPs__h_WXIKzjME6wRQxTBayfWcXuA6loTdSXnV5Tfl63y62nYZUesh2ElcF5ZK_if3R70jTh-_dxAc8-EcxagAgeCAYNGx-i0fokA4Crfz0A9MKJv5XKn8hY-_4_AT-tsIwBQ6YWYISPBGqyUFoBX0LGDEojIywQB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ligNALdOb82FgZzYv5ZKbFMo2s9kwzAb4PdwyaZjIP4_UJiJBqOazslWJYpdMLY2ZHGwfg0ZFP3adV06hbR1i9bJ7lyew2Dalqh9Sq-cFPg32-6rbZXYJnFFzfMks4FOJCgPOhCz2QODdbZU8_nQp_EQ4mIXnD2BR6BcCa3odD8rliM3IYOWnq3AhCHen55FwygCw6-u61Q03m4httHFozE-x4YiQivTQ70YUmdYNMyU_chQ3WfiJzaOgEwjq8vIP0hasxiaE1fBG7PncjGCUNB9sFiyVYjmSbP68iIQFqQ10oo9hnMYVxs_nSdem23fDNOutoikzjSHPo4_qxg-hNV0GDIrzBPLGsGw_W9agnUQOpwtpYycZ9v472FWhI5z0c8-8U7D9cV9s0ELyO9U7_vHhSoZJCq1edIFVVKTHsVfR2-vOlCHuqlbH4TzqGtIzf-5nUuY7HQ4iIxdjOrfU2GtMaU_tB8v3ZsC2Nr7IMYeyeBhItOXccNnqHUhMyZ-LdHkoafendmWTnnMOlkK2l91Jil4tPrJyVn1YFlsbzobQOurrsz2MAW3Ew1ibTCsRvlKWRs8dOSAxoUBcQL-r9g5_BcF0wsV7xzWvIHFm7fOuqLGsjsrCMAuXKImA2tFYECpr6vqCq8ORdqapUOlMb0_K4Iae-AhEkbMVERgLS3O05tRhQATMl2jnqA71DImzDoQMC_c45ELXToqX7x0oiINsOLQKBFQjiQ4-G9C1DJlZ67ZVtSN2PyKVNqMNegEtSz1uieOmkoXa1Vq62CrDp7KORToTHMZEV9uWe7KAOzqYm7jwhEvFs2jgvSga9Au2YgzqxbBjpC8RDSQqhUZTQeg2gecFLRpIObQ._YXzEw0gT4HbLqcFfdWoFQ")
                request.addHeader("Content-Type", "application/json")
                request.addHeader("X-Requested-With", "XMLHttpRequest")
                request.addHeader("Cookie", "XMLHttpRequest")
                chain.proceed(request.build())
            }

            val retrofit = Retrofit.Builder()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClientBuilder.build())
                .baseUrl(Constants.BASE_URL)
                .build()

            return retrofit.create(
                WebServices::
                class.java
            )
        }
    }

这是我的错误:

D/OkHttp: --> GET http://pilot.wakecap.com/api/sites/10010001/workers
D/OkHttp: --> END GET
I/art: Do partial code cache collection, code=60KB, data=57KB
I/art: After code cache collection, code=58KB, data=56KB
I/art: Increasing code cache capacity to 256KB
D/OkHttp: <-- 401 Unauthorized https://pilot.wakecap.com/api/sites/10010001/workers (2645ms)
D/OkHttp: Server: nginx/1.14.0 (Ubuntu)
D/OkHttp: Date: Wed, 16 Jan 2019 20:16:44 GMT
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: Content-Length: 83
D/OkHttp: Connection: keep-alive
D/OkHttp: X-DNS-Prefetch-Control: off
D/OkHttp: X-Frame-Options: SAMEORIGIN
D/OkHttp: Strict-Transport-Security: max-age=15552000; includeSubDomains
D/OkHttp: X-Download-Options: noopen
D/OkHttp: X-Content-Type-Options: nosniff
D/OkHttp: X-XSS-Protection: 1; mode=block
D/OkHttp: Access-Control-Allow-Origin: *
D/OkHttp: ETag: W/"53-ocmggiYI6s3nDDtb9FHiqlrJJYU"
D/OkHttp: {"code":"unauthenticated","message":"Server Error. Try again later!!","status":401}
D/OkHttp: <-- END HTTP (83-byte body)

PS:我也使用过Cookie,但没有帮助 PS2:它说稍后再试,但与Postman一起使用时效果很好 PS3:出于某些原因,Volley可以正常工作:|

1 个答案:

答案 0 :(得分:0)

所以我发现了问题所在。

问题是我必须使用“ https”,而我使用的是http,显然Volley和Postman都自动在末尾添加s,但是Retrofit出于某些原因没有添加。