Why am I getting 404 error when requesting token? (kotlin/android/retrofit)
(TEMPLATE)
POST /auth/token HTTP/1.1
Host: oauth.wildapricot.org
Authorization: Basic QVBJS0VZOm85c2U4N3Jnb2l5c29lcjk4MDcwOS0=
Content-type: application/x-www-form-urlencoded
granttype=clientcredentials&scope=auto
(My Attempt)--------------------------------------------------------------------
object WAApiCallService {
private const val APIKEY = "xxxxxxxxxxxxxxxxxxxx"
private const val BASEURL = "https://oauth.wildapricot.org"
private val AUTH = "Basic" + Base64.encodeToString(APIKEY.toByteArray(), Base64.NOWRAP)
private const val CONTENT_TYPE = "application/x-www-form-urlencoded"
private var api:WAApiCall? = null
private fun getWAApi(context: Context) : WAApiCall {
if(api==null){
val OkHttpClient = OkHttpClient.Builder()
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BODY
if(BuildConfig.DEBUG){
OkHttpClient.addInterceptor(logging)
}
//define and add a cache
val cacheSize = 5 * 1024 * 1024L // = 5mb cache
val cache = Cache(context.cacheDir, cacheSize)
//add cache to client
OkHttpClient.cache(cache)
//add custom interceptor
OkHttpClient.addInterceptor{chain ->
//get the outgoing request
val request = chain.request()
val newRequest = request.newBuilder()
.addHeader("Authorization", AUTH)
.addHeader("Content-type", CONTENT_TYPE)
.build()
chain.proceed(newRequest)
}
api = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(OkHttpClient.build())
.build()
.create(WAApiCall::class.java)
}
return api!!
}
fun call(context: Context) =
getWAApi(context)
}
(The Call Interface)-------------------------------------------------------
interface WAApiCall {
@POST("auth/token HTTP/1.1")
fun callPost(@Body body:String ): Call<AuthToken>
}
(RESPONSE DATA CLASS)--------------------------------------------------------
data class AuthToken(
@SerializedName("accesstoken")
var accessToken: String,
@SerializedName("expiresin")
var expiresIn: Int,
@SerializedName("permissions")
var permissions: List<Permission>,
@SerializedName("refreshtoken")
var refreshToken: String,
@SerializedName("tokentype")
var tokenType: String
)
(FUNCTION IN MAIN ACTIVITY THAT I CALL)-----------------------------------------
fun testRequest(){
val call = WAApiCallService.call(this)
call.callPost("granttype=clientcredentials&scope=auto&obtainrefreshtoken=true")
.enqueue(object: Callback<AuthToken>{
override fun onFailure(call: Call<AuthToken>, t: Throwable) {
Log.i("FAILURE", t.localizedMessage)
}
override fun onResponse(call: Call<AuthToken>, response: Response<AuthToken>) {
Log.i("SUCCESS", "TOKEN = ${response.body()}")
Log.i("SUCCESS", "${response}")
}
})
}
(THE RESPONSE I GET)-----------------------------------------------------------
I/SUCCESS: TOKEN = null
I/SUCCESS: Response{protocol=http/1.1, code=404, message=Not Found,
url=https://oauth.wildapricot.org/auth/token%20HTTP/1.1}
API endpoint should be https://oauth.wildapricot.org/auth/token
but you post to https://oauth.wildapricot.org/auth/token%20HTTP/1.1