简体   繁体   中英

Trouble getting Meetup APIs Access Token with Retrofit - Android

I am trying to implement OAuth 2 authentication for the Meetup API using retrofit. I have the Authorization code but I am having trouble getting the Access Token. Here is all the relevant bits of code:

My onResume method:

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)
        }
    }
}

Login Service interface:

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

}

RequestBody class:

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

Relevant methods from the service generator class:

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);
}

Why does the response to the access token POST give me a 400 response instead of a JSON with a access token like it desribes here ? What am I missing ?

Using the @Body annotation means it will use the registered Converter to serialize the body, so it is not sending it as the documentation describes it, but as a JSON object encoded through GSON.

You need to use the "form-encoded" method to send the data, look for the "form encoded and multipart" section of the retrofit documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM