0

Using Java, making a simple "delivery_quotes" call for testing which return HTTP response code - 403

Below is the code:

String api_key =  Base64.getEncoder().encodeToString("95xxxxx1-xxxx-44h1-8961-904xxx024fb:".getBytes("utf-8"));
String postData = "pickup_address=" + java.net.URLEncoder.encode("20 McAllister St, San Francisco, CA", "UTF-8") + "&" + 
              "dropoff_address=" + java.net.URLEncoder.encode("101 Market St, San Francisco, CA", "UTF-8"); 
String apiURL = "https://api.postmates.com/v1/customers/cus_LxxxxRaHNT_yqV/delivery_quotes";
URL myurl = new URL(apiURL);

HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Authorization", "Basic " + api_key);

OutputStream os = con.getOutputStream();
os.write(postData.getBytes());

InputStream istream = con.getInputStream();
int ch;
while((ch=istream.read()) != -1)
{
  System.out.print((char)ch);
}

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://api.postmates.com/v1/customers/cus_LBxxxxxxNT_yqV/delivery_quotes

Customer ID and Test API keys which I am using are correct. Any HELP!

user1486269
  • 41
  • 1
  • 1
  • 9
  • Are you sure that the api key ends with `:`? – Hannes Mar 21 '17 at 18:47
  • Yes it end with ":" and password is blank. – user1486269 Mar 21 '17 at 18:54
  • First have a look at http://stackoverflow.com/questions/11458891/urlconnection-setrequestproperty-vs-addrequestproperty. And then check the `api_key` again. – Hannes Mar 21 '17 at 19:09
  • Are you sure the URL is correct? I'm asking because HTTP 403 means forbidden access. – dsp_user Mar 21 '17 at 19:24
  • I checked the api_key again, it includes the ":" at the end before I encode it in base64 as you can see it in the first line of the code I pasted in the question. I also tried with no luck by adding "Accept" property and set it to "application/json" – user1486269 Mar 21 '17 at 19:27
  • URL seems correct to me as the only thing I am replacing is the customer Id from the following URL: https://api.postmates.com/v1/customers/:customer_id/delivery_quotes – user1486269 Mar 21 '17 at 19:32

1 Answers1

1

This is what worked for me:

  1. Put the customer id in the url, as you have done correctly.
  2. Put the Sandbox into the username field.
  3. Then set the Secret into the password field (on the right side of the :colon).
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Will Said
  • 394
  • 4
  • 7