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());