请求标头不随之而来的Retrofit 2.6.0 Android Kotlin

发布于 2025-01-30 20:01:23 字数 6526 浏览 3 评论 0原文

我试图通过授权令牌通过我们的应用程序致电API。我尝试了所有无运气的解决方案。

这是我使用OKHTTP

companion object {
        var token: String = "token"
        var apiService: ApiService? = null

        fun getInstance(): ApiService {

            val httpClient = OkHttpClient.Builder()
                .addInterceptor(Interceptor(token))
                .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))

            if (apiService == null) {

                apiService = Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(httpClient.build())
                    .build().create(ApiService::class.java)
            }
            return apiService!!
        }
    }

Interceptor

class Interceptor(
    private val token: String
) : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        request = request.newBuilder()
            .header("Authorization", "Bearer $token")
            .header("Accept", "application/json")
            .header("Content-Type", "application/json")
            .build()
        return chain.proceed(request)
    }
}

API

// get all users
    @GET("api/user/get_all_users.php")
    suspend fun getSpaces(
    ): Response<UserListResponse>

调用的Raterofit客户端,我尝试发送一个动态标头时,

// get all users
    @GET("api/user/get_all_users.php")
    suspend fun getSpaces(
        @Header("Authorization") token: String = "Bearer 2|ec131b10fb15295445e8075dfa8883b5"
    ): Response<UserListResponse>

我尝试发送一个硬编码的标头AS

// get all users
    @Headers("Authorization: Bearer 2|ec131b10fb15295445e8075dfa8883b5")
    @GET("api/user/get_all_users.php")
    suspend fun getSpaces(): Response<UserListResponse>

,也可以与Interceptor一起尝试。我不知道我在哪里做错。

这是请求的标题记录,

2022-05-21 01:39:24.839 30140-30140/com.test.myapp D/Called: 0
2022-05-21 01:39:25.091 30140-30567/com.test.myapp I/okhttp.OkHttpClient: --> GET https://test_domain.com/test/api/user/get_all_users.php
2022-05-21 01:39:25.091 30140-30567/com.test.myapp I/okhttp.OkHttpClient: Authorization: Bearer 2|ec131b10fb15295445e8075dfa8883b5
2022-05-21 01:39:25.092 30140-30567/com.test.myapp I/okhttp.OkHttpClient: Accept: application/json
2022-05-21 01:39:25.092 30140-30567/com.test.myapp I/okhttp.OkHttpClient: Content-Type: application/json
2022-05-21 01:39:25.092 30140-30567/com.test.myapp I/okhttp.OkHttpClient: --> END GET
2022-05-21 01:39:25.436 30140-30140/com.test.myapp I/Choreographer: Skipped 49 frames!  The application may be doing too much work on its main thread.
2022-05-21 01:39:25.438 30140-30417/com.test.myapp I/OpenGLRenderer: Davey! duration=871ms; Flags=0, IntendedVsync=197729743820535, Vsync=197729777153869, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=197729789299244, AnimationStart=197729789306552, PerformTraversalsStart=197730340706860, DrawStart=197730341252552, SyncQueued=197730612659168, SyncStart=197730612991321, IssueDrawCommandsStart=197730613160552, SwapBuffers=197730614595937, FrameCompleted=197730616140629, DequeueBufferDuration=220769, QueueBufferDuration=467692, GpuCompleted=0, 
2022-05-21 01:39:25.821 30140-30140/com.test.myapp D/Called: 0
2022-05-21 01:39:25.827 30140-30570/com.test.myapp I/okhttp.OkHttpClient: --> GET https://test_domain.com/test/api/user/get_all_users.php
2022-05-21 01:39:25.827 30140-30570/com.test.myapp I/okhttp.OkHttpClient: Authorization: Bearer 2|ec131b10fb15295445e8075dfa8883b5
2022-05-21 01:39:25.827 30140-30570/com.test.myapp I/okhttp.OkHttpClient: Accept: application/json
2022-05-21 01:39:25.828 30140-30570/com.test.myapp I/okhttp.OkHttpClient: Content-Type: application/json
2022-05-21 01:39:25.828 30140-30570/com.test.myapp I/okhttp.OkHttpClient: --> END GET
2022-05-21 01:39:25.862 30140-30567/com.test.myapp D/Linux: [Posix_connect Debug]Process com.test.myapp :443 
2022-05-21 01:39:25.862 30140-30570/com.test.myapp D/Linux: [Posix_connect Debug]Process com.test.myapp :443 
2022-05-21 01:39:27.038 30140-30570/com.test.myapp I/okhttp.OkHttpClient: <-- 200 https://test_domain.com/test/api/user/get_all_users.php (1210ms)
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: <-- 200 https://test_domain.com/test/api/user/get_all_users.php (1945ms)
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: date: Fri, 20 May 2022 20:09:28 GMT
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: date: Fri, 20 May 2022 20:09:28 GMT
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: content-type: text/html; charset=UTF-8
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: content-type: text/html; charset=UTF-8
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: server: awex
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: server: awex
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: x-xss-protection: 1; mode=block
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: x-xss-protection: 1; mode=block
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: x-content-type-options: nosniff
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: x-content-type-options: nosniff
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: x-request-id: 453cf99926f1552c1bdb630482505474
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: x-request-id: 5e903f039cec6ceacf28bc3209a19462
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: <-- END HTTP
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: <-- END HTTP
2022-05-21 01:39:27.062 30140-30140/com.test.myapp D/Response: UserListResponse(data=null, message=Unauthorized access denied!, status=failure, statusCode=401)
2022-05-21 01:39:27.063 30140-30140/com.test.myapp D/Response: UserListResponse(data=null, message=Unauthorized access denied!, status=failure, statusCode=401)

添加了我的Postman请求的响应,该请求正常

I am trying to call an API through our app with an authorization token. I have tried all available solutions with no luck.

Here is my retrofit client with okhttp

companion object {
        var token: String = "token"
        var apiService: ApiService? = null

        fun getInstance(): ApiService {

            val httpClient = OkHttpClient.Builder()
                .addInterceptor(Interceptor(token))
                .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))

            if (apiService == null) {

                apiService = Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(httpClient.build())
                    .build().create(ApiService::class.java)
            }
            return apiService!!
        }
    }

Interceptor

class Interceptor(
    private val token: String
) : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        request = request.newBuilder()
            .header("Authorization", "Bearer $token")
            .header("Accept", "application/json")
            .header("Content-Type", "application/json")
            .build()
        return chain.proceed(request)
    }
}

Api Call

// get all users
    @GET("api/user/get_all_users.php")
    suspend fun getSpaces(
    ): Response<UserListResponse>

I tried sending a dynamic header as

// get all users
    @GET("api/user/get_all_users.php")
    suspend fun getSpaces(
        @Header("Authorization") token: String = "Bearer 2|ec131b10fb15295445e8075dfa8883b5"
    ): Response<UserListResponse>

I tried sending a hardcoded header as

// get all users
    @Headers("Authorization: Bearer 2|ec131b10fb15295445e8075dfa8883b5")
    @GET("api/user/get_all_users.php")
    suspend fun getSpaces(): Response<UserListResponse>

And, Also tried with the interceptor. I don't know where I am doing a mistake.

Here is the header logging of the request

2022-05-21 01:39:24.839 30140-30140/com.test.myapp D/Called: 0
2022-05-21 01:39:25.091 30140-30567/com.test.myapp I/okhttp.OkHttpClient: --> GET https://test_domain.com/test/api/user/get_all_users.php
2022-05-21 01:39:25.091 30140-30567/com.test.myapp I/okhttp.OkHttpClient: Authorization: Bearer 2|ec131b10fb15295445e8075dfa8883b5
2022-05-21 01:39:25.092 30140-30567/com.test.myapp I/okhttp.OkHttpClient: Accept: application/json
2022-05-21 01:39:25.092 30140-30567/com.test.myapp I/okhttp.OkHttpClient: Content-Type: application/json
2022-05-21 01:39:25.092 30140-30567/com.test.myapp I/okhttp.OkHttpClient: --> END GET
2022-05-21 01:39:25.436 30140-30140/com.test.myapp I/Choreographer: Skipped 49 frames!  The application may be doing too much work on its main thread.
2022-05-21 01:39:25.438 30140-30417/com.test.myapp I/OpenGLRenderer: Davey! duration=871ms; Flags=0, IntendedVsync=197729743820535, Vsync=197729777153869, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=197729789299244, AnimationStart=197729789306552, PerformTraversalsStart=197730340706860, DrawStart=197730341252552, SyncQueued=197730612659168, SyncStart=197730612991321, IssueDrawCommandsStart=197730613160552, SwapBuffers=197730614595937, FrameCompleted=197730616140629, DequeueBufferDuration=220769, QueueBufferDuration=467692, GpuCompleted=0, 
2022-05-21 01:39:25.821 30140-30140/com.test.myapp D/Called: 0
2022-05-21 01:39:25.827 30140-30570/com.test.myapp I/okhttp.OkHttpClient: --> GET https://test_domain.com/test/api/user/get_all_users.php
2022-05-21 01:39:25.827 30140-30570/com.test.myapp I/okhttp.OkHttpClient: Authorization: Bearer 2|ec131b10fb15295445e8075dfa8883b5
2022-05-21 01:39:25.827 30140-30570/com.test.myapp I/okhttp.OkHttpClient: Accept: application/json
2022-05-21 01:39:25.828 30140-30570/com.test.myapp I/okhttp.OkHttpClient: Content-Type: application/json
2022-05-21 01:39:25.828 30140-30570/com.test.myapp I/okhttp.OkHttpClient: --> END GET
2022-05-21 01:39:25.862 30140-30567/com.test.myapp D/Linux: [Posix_connect Debug]Process com.test.myapp :443 
2022-05-21 01:39:25.862 30140-30570/com.test.myapp D/Linux: [Posix_connect Debug]Process com.test.myapp :443 
2022-05-21 01:39:27.038 30140-30570/com.test.myapp I/okhttp.OkHttpClient: <-- 200 https://test_domain.com/test/api/user/get_all_users.php (1210ms)
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: <-- 200 https://test_domain.com/test/api/user/get_all_users.php (1945ms)
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: date: Fri, 20 May 2022 20:09:28 GMT
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: date: Fri, 20 May 2022 20:09:28 GMT
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: content-type: text/html; charset=UTF-8
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: content-type: text/html; charset=UTF-8
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: server: awex
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: server: awex
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: x-xss-protection: 1; mode=block
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: x-xss-protection: 1; mode=block
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: x-content-type-options: nosniff
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: x-content-type-options: nosniff
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: x-request-id: 453cf99926f1552c1bdb630482505474
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: x-request-id: 5e903f039cec6ceacf28bc3209a19462
2022-05-21 01:39:27.039 30140-30570/com.test.myapp I/okhttp.OkHttpClient: <-- END HTTP
2022-05-21 01:39:27.039 30140-30567/com.test.myapp I/okhttp.OkHttpClient: <-- END HTTP
2022-05-21 01:39:27.062 30140-30140/com.test.myapp D/Response: UserListResponse(data=null, message=Unauthorized access denied!, status=failure, statusCode=401)
2022-05-21 01:39:27.063 30140-30140/com.test.myapp D/Response: UserListResponse(data=null, message=Unauthorized access denied!, status=failure, statusCode=401)

Adding response of my postman request which is working fine
Postman Response

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

起风了 2025-02-06 20:01:23

这不是改造的实现问题。这是我们的后端API中的错别字问题。

我使用PHP使用了一个自编写的API库,并实施了授权以防止未经授权的API调用。在执行授权时,我忘记处理请求标题中的授权标题的案例敏感性。在改造的情况下,授权标题的关键是授权,而在Postman的情况下,它是授权

It wasn't an implementation problem of retrofit. It was a typo problem in our backend APIs.

I was using a self-written API library using PHP and implemented the authorization to prevent unauthorized API calls. In the implementation of authorization, I forget to deal with the case sensitivity of authorization header coming in request headers. In the case of retrofit, the Key for the authorization header is Authorization whereas in the case of postman it was authorization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文