this is my first time posting here, if there are some bad formatting please let me know.
So I am currently developing an android app for this course i am taking and i am supposed to register, login and then post an image with description to the given API using Retrofit. The login and register part works perfectly and i am able to pass the token to the POST method (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)
}
}
This is what the logcat says
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:
It is said there that the authorization already has a token value passed to it, however at the end of the logcat it says
<-- 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)
Any help would be greatly appreciated.
Here is the API documentation: https://story-api.dicoding.dev/v1/#/
Solved, it had to put "Bearer" before the token.
val tokenConcatenate = "Bearer "+tokenTemp
//TOKENTEMP ALREADY EQUALS TOKEN HERE
Log.d(TAG, "uploadImageTOKEN: $tokenConcatenate")
val service = tokenTemp?.let {
ApiConfig().getApiService().addStory(
imageMultipart,
description,
tokenConcatenate
)
}
and then pass the value to the API service