在 POST 结束时改造缺少的身份验证

尼古拉斯·哈杜

这是我第一次在这里发帖,如果有一些错误的格式,请告诉我。

所以我目前正在为我正在学习的这门课程开发一个 android 应用程序,我应该注册、登录,然后使用 Retrofit 将带有描述的图像发布到给定的 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/#/

尼古拉斯·哈杜

解决了,它必须在令牌前加上“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 服务

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在使用Python Requests模块向YouTube API v3发出POST请求时,为什么会收到“请求缺少必需的身份验证凭据”的信息?

IE在使用NTLM身份验证时(使用Spring的角度)随机发送空的POST正文

使用 POST 请求的身份验证无法验证数据

MSAL 身份验证:POST 请求正文中缺少 401 和 client_assertion' 或 'client_secret'

POST请求的身份验证错误:使用Axios时未提供“身份验证凭据”,但使用POSTMAN时有效

如何使用令牌身份验证对 Post 请求进行身份验证?

OpenID:未经身份验证的POST在身份验证后变为GET

$ .ajax POST提示身份验证对话框

无法自动执行curl POST身份验证的Bash功能

Android Retrofit GET和POST如何使用身份验证

使用Javascript发出HTTP POST身份验证基本请求

Django会话身份验证和Axios POST信号

CAS身份验证后恢复原始POST请求?

通过POST使用请求库进行python身份验证

使用 spring restTemplate 对 POST REST API 进行身份验证

PHP CURL GET / POST摘要身份验证

带有身份验证令牌的httr POST请求

POST 400错误请求React身份验证

GET 和 POST 上的不同身份验证

通过post从api检索身份验证令牌

在HTTP Post上检查用户身份验证

Route.post()需要回调函数,但在使用Passport身份验证时得到了[object Undefined]

错误此路由不支持GET方法。支持的方法:POST。尝试使用路由而不提供身份验证令牌时

访问API网关时缺少身份验证令牌?

使用POST的NodeJS Express身份验证发送后返回错误无法设置标头

针对用户身份验证的Swift POST或GET请求iOS Swift

GET POST更改后身份验证不再起作用

python 请求:如何通过代理对 post 和访问文件进行身份验证

rest api post方法不适用于meanjs 0.5.0中的基本身份验证