无法获取Meetup API访问令牌与改造 – Android

我正在尝试使用改进来实现Meetup API的 OAuth 2身份验证。 我有授权码,但我无法获得访问令牌。 这里是所有相关的代码位:

我的onResume方法:

override fun onResume() { super.onResume() // the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent val uri = intent.data if (uri != null && uri.toString().startsWith(CALL_BACK)) { val code = uri.getQueryParameter("code") if (code != null) { // get access token // we'll do that in a minute //"authorize code: $code".show(this) Log.i("Rakshak","Code: $code") // The Authorization Code is printed val loginService = ServiceGenerator.createService(LoginService::class.java) //var request = RequestBody(CLIENT_ID,CLIENT_SECRET,"authorization_code",CALL_BACK,code) val requestBody = "client_id=$CLIENT_ID"+ "&client_secret=$CLIENT_SECRET"+ "&grant_type=authorization_code"+ "&redirect_uri=$CALL_BACK+" + "&code=$code" val call = loginService.getAccessToken(requestBody) //val accessToken = call.execute().body() call.enqueue(object : retrofit2.Callback<AccessToken>{ override fun onResponse(call: Call<AccessToken>?, response: Response<AccessToken>?) { Log.i("Rakshak","Response: ${response.toString()}") // Prints: "Response{protocol=h2, code=400, message=, url=https://secure.meetup.com/oauth2/access}" Log.i("Rakshak","Access token: ${response?.body()?.accessToken}")// Prints: "Access token: null" } override fun onFailure(call: Call<AccessToken>?, t: Throwable?) { Log.i("Rakshak","Didn't work. ${t?.localizedMessage}") } }) } else if (uri.getQueryParameter("error") != null) { // show an error message here "Didn't work. Code: $code".show(this) } } } 

登录服务界面:

 interface LoginService { @FormUrlEncoded @POST("https://secure.meetup.com/oauth2/access") fun getAccessToken(@Field("body") requestBody: String): Call<AccessToken> 

}

RequestBody类:

 data class RequestBody( var client_id:String, var client_secret: String, var grant_type: String, var redirect_uri: String, var code: String) 

服务生成器类的相关方法:

 private val API_BASE_URL = "https://secure.meetup.com/oauth2/access/" private val httpClient = OkHttpClient.Builder() private val builder = Retrofit.Builder() .baseUrl(API_BASE_URL) .addConverterFactory(GsonConverterFactory.create()) private var retrofit = builder.build() fun <S> createService(serviceClass: Class<S>): S { return retrofit.create(serviceClass); } 

为什么访问令牌POST的响应给我一个400响应,而不是带有访问令牌的JSON呢? 我错过了什么?

使用@Body注解意味着它将使用已注册的Converter序列化主体,所以它不会像文档中描述的那样发送它,而是作为通过GSON编码的JSON对象。

您需要使用“表格编码”方法发送数据,查找改进文档的“表格编码和多部分”部分。