这是我第一次在这里发帖,如果有一些错误的格式,请告诉我。
所以我目前正在为我正在学习的这门课程开发一个 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] 删除。
我来说两句