[英]Sending a header for authorization with Volley in Android using Kotlin
I am trying to authenticate users via verification code sent to their emails after the submitting details for sign up.我正在尝试通过在提交注册详细信息后发送到他们的电子邮件的验证码来验证用户。
I have the following code to listen to the code/pin as received via the email but am not sure if I am adding the header/bearer-token the right way as I receive server error on verification.我有以下代码来收听通过电子邮件收到的代码/pin,但我不确定我是否以正确的方式添加标头/承载令牌,因为我在验证时收到服务器错误。
Kindly assist请协助
var codeEntered:String? = null
txt_pin_entry.setOnPinEnteredListener {
codeEntered = "$it"
Toast.makeText(context!!.applicationContext, "inside $codeEntered", Toast.LENGTH_SHORT).show()
}
submitBtn.setOnClickListener {
val url = "https://fathomless-badlands-69782.herokuapp.com/api/auth/verify"
val params = HashMap<String, String?>()
params["Authorization"] = "Bearer $tokenCode"
params["code"] = codeEntered
Toast.makeText(context!!.applicationContext, "outside $codeEntered", Toast.LENGTH_SHORT).show()
val jsonObject = JSONObject(params as Map<*, *>)
val request = JsonObjectRequest(Request.Method.PUT, url, jsonObject,
Response.Listener { response ->
try{
Log.i("Registration", "Response $response")
}catch (e: Exception){
Log.e("Registration", "Response $e")
Toast.makeText(context!!.applicationContext, "Response $response", Toast.LENGTH_SHORT).show()
}
}, Response.ErrorListener {
Log.e("Registration", "Response $it")
Toast.makeText(context!!.applicationContext, "Response $it", Toast.LENGTH_SHORT).show()
})
VolleySingleton.getInstance(context!!.applicationContext).addToRequestQueue(request)
val actionToVerificationResult = CodeVerificationFragmentDirections.actionToVerificationResult()
Navigation.findNavController(it).navigate(actionToVerificationResult)
}
}
The API expects a request as below API 需要如下请求
{
code: String
}
Did you try this below one你在下面试过这个吗
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
headers["code"] = Your code
return headers
}
example:例子:
class ServiceVolley : ServiceInterface {
val TAG = ServiceVolley::class.java.simpleName
val basePath = "https://your/backend/api/"
override fun post(path: String, params: JSONObject, completionHandler: (response: JSONObject?) -> Unit) {
val jsonObjReq = object : JsonObjectRequest(Method.POST, basePath + path, params,
Response.Listener<JSONObject> { response ->
Log.d(TAG, "/post request OK! Response: $response")
completionHandler(response)
},
Response.ErrorListener { error ->
VolleyLog.e(TAG, "/post request fail! Error: ${error.message}")
completionHandler(null)
}) {
@Throws(AuthFailureError::class)
override fun getHeaders(): Map<String, String> {
val headers = HashMap<String, String>()
headers.put("Content-Type", "application/json")
return headers
}
}
BackendVolley.instance?.addToRequestQueue(jsonObjReq, TAG)
}
}
Return header orverridding getHeaders返回标头或覆盖 getHeaders
val request = JsonObjectRequest(Request.Method.PUT, url, jsonObject,
Response.Listener { response ->
try{
Log.i("Registration", "Response $response")
}catch (e: Exception){
Log.e("Registration", "Response $e")
Toast.makeText(context!!.applicationContext, "Response $response", Toast.LENGTH_SHORT).show()
}
}, Response.ErrorListener {
Log.e("Registration", "Response $it")
Toast.makeText(context!!.applicationContext, "Response $it", Toast.LENGTH_SHORT).show()
}){
@Throws(AuthFailureError::class)
override fun getHeaders(): Map<String, String> {
val headers = HashMap<String, String>()
headers.put("Content-Type", "application/json")
headers.put("Authorization", "Bearer $tokenCode")
return headers
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.