Click here to Skip to main content
15,121,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to call razorpay api to create order but when I create got 401 error i.e. authentication error
please help me i tried following code

What I have tried:

public String CreateOrder(String strAmt){


       String strRtn = "";

       try {

           final String POST_PARAMS = "{\n" + "\"amount\": \""+strAmt+"\",\r\n" + "\"currency\": \"INR\",\r\n" + "}";

           System.out.println("################ " + POST_PARAMS);
           String spec = "https://api.razorpay.com/v1/orders";
           URL obj = new URL(spec);
           HttpURLConnection postConnection = (HttpURLConnection) obj.openConnection();
          postConnection.setRequestMethod("POST");

           postConnection.setRequestProperty("username", "rzp_test_ABC");
           postConnection.setRequestProperty("password", "234324324324");
           postConnection.setRequestProperty("Content-Type", "application/json");
           postConnection.setDoOutput(true);

           OutputStream os = postConnection.getOutputStream();
           os.write(POST_PARAMS.getBytes());
           os.flush();
           os.close();

           int responseCode = postConnection.getResponseCode();


           InputStream in = new BufferedInputStream(postConnection.getInputStream());
           BufferedReader reader = new BufferedReader(new InputStreamReader(in));
           StringBuilder result = new StringBuilder();
           String line;
           while ((line = reader.readLine()) != null) {
               result.append(line);
           }
           strRtn= result.toString();


       }
       catch (Exception ex) {
           strRtn = "ERROR IN RAZOPay API : " + ex.toString();
           ex.printStackTrace();
       }
       return strRtn;
   }
Posted
Updated 30-Dec-20 4:30am
Comments
Richard MacCutchan 30-Dec-20 4:54am
   
You need to ask razorpay. We have no idea what part of your request is being rejected by them.

1 solution

You should always start with third party documentation: API Reference Guide[^]

401 means its unauthorized request. You probably have not followed the right way to associate the auth keys.

Quote:
All successful responses are returned with HTTP Status code 200. In case of failure, Razorpay API returns a JSON error response with the parameters that detail the reason for the failure.


BTW, I have never seen any decent service asking for username-password in plain as part of request.

Quote:
All Razorpay APIs are authorized using Basic Authorization. Basic authorization requires the following:

<your_key_id>
<your_key_secret>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900