【发布时间】:2020-09-09 19:40:36
【问题描述】:
当我尝试在改造的帮助下获取带有客户端 ID、客户端密钥和授权码的令牌时。我收到 401 UnAuthorized 错误
MainActivity.kt
if (appLinkData != null && appLinkData.toString().startsWith("https://abc.abconline.com/plugin/callback")) {
val code = appLinkData.getQueryParameter("code")
// Toast.makeText(this, code, Toast.LENGTH_SHORT).show()
val builder = Retrofit.Builder().baseUrl("https://abc.abcOnline.com")
.addConverterFactory(GsonConverterFactory.create()).build()
val client = builder.create(restClient::class.java)
client.getAccessToken(
clientId,
clientSecret,
code,
"Authorization"
).enqueue(object : Callback<AccessToken> {
override fun onFailure(call: Call<AccessToken>, t: Throwable) {
Toast.makeText(applicationContext, "Falied: $t.message", Toast.LENGTH_SHORT)
.show()
}
override fun onResponse(call: Call<AccessToken>, response: Response<AccessToken>) {
Toast.makeText(
applicationContext,
response.body().toString(),
Toast.LENGTH_SHORT
).show()
}
RestClient.kt
interface restClient {
@POST("/oauth2/token")
@FormUrlEncoded
fun getAccessToken(
@Field("client_id") clientId: String,
@Field("client_secret") clientSecret: String,
@Field("code") code: String,
@Field("grant_type") grantType: String,
@Field("redirect_uri") redirectUri: String
): Call<AccessToken>
}
我该如何解决? 我花了很多时间来弄清楚......
谢谢
【问题讨论】:
标签: android kotlin oauth-2.0 retrofit2