需要凌空授权

我正在尝试转换 Unirest


   HttpResponse<String> response = Unirest.post("https://api.tap.company/v2/charges")

  .header("authorization", "Bearer sk_test_XKokBfNWv6FIYuTMg5sLPjhJ")

  .header("content-type", "application/json")

  .body("{\"amount\":1,\"currency\":\"KWD\",\"receipt\":{\"email\":false,\"sms\":true},\"customer\":{\"first_name\":\"test\",\"phone\":{\"country_code\":\"965\",\"number\":\"50000000\"}},\"source\":{\"id\":\"src_kw.knet\"},\"redirect\":{\"url\":\"http://your_website.com/redirect_url\"}}")

  .asString();

到凌空


RequestQueue queue = Volley.newRequestQueue(this);


        String url = "https://api.tap.company/v2/charges";

        StringRequest TapREQUEST = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

            @Override public void onResponse(String response) {

                Log.w("OnResponse:", response);

            }

        }, new Response.ErrorListener() {

            @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); }

        }) {

            @Override public Map<String, String> getHeaders() {

                Map<String,String> headers = new HashMap<>();

                headers.put("content-type", "application/json");

                headers.put("authorization", "Bearer sk_test_XKokBfNWv6FIYuTMg5sLPjhJ");

                //String auth = "Bearer " + Base64.encodeToString("sk_test_XKokBfNWv6FIYuTMg5sLPjhJ".getBytes(), Base64.DEFAULT);

                //headers.put("authorization", auth);

                return headers;        

            }

        };

        queue.add(TapREQUEST);

但是我得到E /Volley:[396]BasicNetwork.performRequest:意外响应代码400用于 https://api.tap.company/v2/charges


当我点击链接时,我得到


{“errors”:[{“code”:“2107”,“description”:“Authorization Required”}]}


阿晨1998
浏览 112回答 1
1回答

慕容森

您必须以不同的方式设置正文参数。让我们创建返回正确字符串的方法:@NotNullprivate JSONObject getJsonObject() {&nbsp; &nbsp; JSONObject params = new JSONObject();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; params.put("amount", "1");&nbsp; &nbsp; &nbsp; &nbsp; params.put("currency", "KWD");&nbsp; &nbsp; &nbsp; &nbsp; JSONObject receipt = new JSONObject();&nbsp; &nbsp; &nbsp; &nbsp; receipt.put("email", "false");&nbsp; &nbsp; &nbsp; &nbsp; receipt.put("sms", "true");&nbsp; &nbsp; &nbsp; &nbsp; params.put("receipt", receipt);&nbsp; &nbsp; &nbsp; &nbsp; JSONObject customer = new JSONObject();&nbsp; &nbsp; &nbsp; &nbsp; customer.put("first_name", "test");&nbsp; &nbsp; &nbsp; &nbsp; JSONObject phone = new JSONObject();&nbsp; &nbsp; &nbsp; &nbsp; phone.put("country_code", "965");&nbsp; &nbsp; &nbsp; &nbsp; phone.put("number", "50000000");&nbsp; &nbsp; &nbsp; &nbsp; customer.put("phone", phone);&nbsp; &nbsp; &nbsp; &nbsp; params.put("customer", customer);&nbsp; &nbsp; &nbsp; &nbsp; JSONObject id = new JSONObject();&nbsp; &nbsp; &nbsp; &nbsp; id.put("id", "src_kw.knet");&nbsp; &nbsp; &nbsp; &nbsp; params.put("source", id);&nbsp; &nbsp; &nbsp; &nbsp; JSONObject url = new JSONObject();&nbsp; &nbsp; &nbsp; &nbsp; url.put("url", "http://ib7ar.com");&nbsp; &nbsp; &nbsp; &nbsp; params.put("redirect", url);&nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return params;}现在,您不需要使用方法:getParams()getBody()@Overridepublic byte[] getBody() {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; return getJsonObject().toString().getBytes("utf-8");&nbsp; &nbsp; } catch (UnsupportedEncodingException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java