在 POST 末尾改造缺少身份验证

Retrofit missing authentication at the end of POST

这是我第一次post来到这里,如果有一些格式错误请告诉我。

所以我目前正在为我正在学习的这门课程开发一个 android 应用程序,我应该注册、登录,然后 post 一张带有给定 API 描述的图像使用改造。登录和注册部分工作完美,我能够将令牌传递给 POST 方法 (addStory)

interface ApiService {

    @FormUrlEncoded
    @POST("register")
    fun register(
        @Field("name") name:String,
        @Field("email") email:String,
        @Field("password") password:String
    ): Call<RegisterResponse>

    @FormUrlEncoded
    @POST("login")
    fun login(
        @Field("email") email:String,
        @Field("password") password: String
    ):Call<LoginResponse>

    @Multipart
    @POST("stories")
    fun addStory(
        @Part file: MultipartBody.Part,
        @Part("description") description: RequestBody,
        @Header("Authorization") auth: String//Preferences.Key<String>
    ): Call<FileUploadResponse>

    @GET("stories")
    fun getAllStories(@Header("Authorization") token: String): Call<StoryResponse>

}

class ApiConfig {
    fun getApiService(): ApiService {
        val loggingInterceptor =
            HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
        val client = OkHttpClient.Builder()
            .addInterceptor(loggingInterceptor)
            .build()
        val retrofit = Retrofit.Builder()
            .baseUrl("https://story-api.dicoding.dev/v1/")
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build()
        return retrofit.create(ApiService::class.java)
    }
}

logcat 是这么说的

2022-04-17 09:24:54.871 4179-4179/com.dicoding.picodiploma.loginwithanimation D/ContentValues: uploadImageTOKEN: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJ1c2VyLTZOcFRESFBKbnpvdF8yX1AiLCJpYXQiOjE2NTAxNjIyODV9.esZ9-luWxloG7td2RYrn0goUDcThoRDrr0KIvDSoLy8
2022-04-17 09:24:54.876 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: --> POST https://story-api.dicoding.dev/v1/stories
2022-04-17 09:24:54.876 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: Content-Type: multipart/form-data; boundary=00ef4681-1609-4be5-b66a-a39f4d83b70f
2022-04-17 09:24:54.878 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: Content-Length: 200178
2022-04-17 09:24:54.878 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJ1c2VyLTZOcFRESFBKbnpvdF8yX1AiLCJpYXQiOjE2NTAxNjIyODV9.esZ9-luWxloG7td2RYrn0goUDcThoRDrr0KIvDSoLy8
2022-04-17 09:24:54.887 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: --00ef4681-1609-4be5-b66a-a39f4d83b70f
2022-04-17 09:24:54.887 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: Content-Disposition: form-data; name="photo"; filename="17-Apr-2022.jpg"
2022-04-17 09:24:54.887 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: Content-Type: image/jpeg
2022-04-17 09:24:54.887 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: Content-Length: 199749
2022-04-17 09:24:54.887 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: 

那里说授权已经传递了一个令牌值,但是在 logcat 的末尾它说

<-- 401 Unauthorized https://story-api.dicoding.dev/v1/stories (377ms)
2022-04-17 09:24:55.293 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: Server: nginx/1.18.0 (Ubuntu)
2022-04-17 09:24:55.293 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: Date: Sun, 17 Apr 2022 02:24:58 GMT
2022-04-17 09:24:55.293 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: Content-Type: application/json; charset=utf-8
2022-04-17 09:24:55.293 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: Content-Length: 49
2022-04-17 09:24:55.293 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: Connection: keep-alive
2022-04-17 09:24:55.293 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: content-security-policy: upgrade-insecure-requests
2022-04-17 09:24:55.293 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: referrer-policy: strict-origin-when-cross-origin
2022-04-17 09:24:55.293 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: x-frame-options: DENY
2022-04-17 09:24:55.293 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: x-content-type-options: nosniff
2022-04-17 09:24:55.293 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: x-xss-protection: 1; mode=block
2022-04-17 09:24:55.294 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: vary: origin
2022-04-17 09:24:55.294 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: access-control-expose-headers: WWW-Authenticate,Server-Authorization
2022-04-17 09:24:55.294 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: cache-control: no-cache
2022-04-17 09:24:55.294 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: {"error":true,"message":"Missing authentication"}
2022-04-17 09:24:55.294 4179-4285/com.dicoding.picodiploma.loginwithanimation I/okhttp.OkHttpClient: <-- END HTTP (49-byte body)

如有任何帮助,我们将不胜感激。

这是 API 文档:https://story-api.dicoding.dev/v1/#/

已解决,必须在token前加上“Bearer”。

val tokenConcatenate = "Bearer "+tokenTemp
        //TOKENTEMP ALREADY EQUALS TOKEN HERE
        Log.d(TAG, "uploadImageTOKEN: $tokenConcatenate")
        val service = tokenTemp?.let {
            ApiConfig().getApiService().addStory(
                imageMultipart,
                description,
                tokenConcatenate
            )
        }

然后将值传递给 API 服务