【问题标题】:Trouble getting Meetup APIs Access Token with Retrofit - Android使用 Retrofit 获取 Meetup API 访问令牌时遇到问题 - Android
【发布时间】:2017-09-02 17:48:40
【问题描述】:

我正在尝试使用改造为 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?我错过了什么?

【问题讨论】:

    标签: android kotlin retrofit2 meetup


    【解决方案1】:

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

    需要使用“form-encoded”的方式发送数据,查找改造文档的“form-encoded and multipart”部分。

    【讨论】:

    • 感谢您的回复!结果是聚会支持查询字符串参数。因此,我将登录界面更新为 getAccessToken(@Query("client_id") clientId: String, @Query("client_secret") clientSecret: String, @Query("grant_type") grantType: String, @Query("redirect_uri" ) redirect: String, @Query("code") code: String): Call 并且成功了! :D
    猜你喜欢
    • 2017-10-25
    • 2017-04-22
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 2014-03-08
    • 2015-09-21
    相关资源
    最近更新 更多