我正在尝试使用改造为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,就像它描述here一样?我错过了什么?
答案 0 :(得分:0)
使用@Body
注释意味着它将使用已注册的Converter
来序列化正文,因此它不会像文档描述的那样发送它,而是作为通过GSON编码的JSON对象。
您需要使用“表单编码”方法发送数据,查找改进文档中的“表单编码和多部分”部分。