-4

I am using Sinch and following the tutorial, they have code for sending an SMS via Java. However once I run it I am getting a FileNotFoundException error and the following line:

{"errorCode":40100,"message":"Authorization required"}

I have put the phone number in the e.164 format requested but to no avail. Any help would be great!

 String phoneNumber = "+1" + p.getPhoneNum();
            Log.e("Adapteer", phoneNumber);
            String appKey = "app-key";
            String appSecret = "app-secret";
            String message = " Hello! Your table has been cancelled. Thank you.";

            URL url = new URL("https://messagingapi.sinch.com/v1/sms/" + phoneNumber);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type","application/json");

            String userCredentials = "application\\" + appKey + ":" + appSecret;

            byte[] encoded = Base64.encodeBase64(userCredentials.getBytes());
            String basicAuth = "Basic " + new String(encoded);
            connection.setRequestProperty("Authorization", basicAuth);

            String postData = "{\"Message\":\"" + message + "\"}";
            OutputStream os = connection.getOutputStream();
            os.write(postData.getBytes());

            StringBuilder response = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String line;
            while((line= br.readLine()) != null)
                response.append(line);

            br.close();
            os.close();

            System.out.println(response.toString());
Rookie007
  • 1,229
  • 2
  • 18
  • 50

1 Answers1

0

If that is a live key you are using you need to sign the request, we only allow basic authorization on sandbox apps. for more info https://www.sinch.com/tutorials/sign-requests-java/ and https://www.sinch.com/docs/sms/#Authorization

frals
  • 810
  • 4
  • 12
cjensen
  • 2,703
  • 1
  • 16
  • 15