我正在使用Sinch
并遵循本教程,他们有通过 Java 发送 SMS 的代码。但是,一旦我运行它,我就会得到一个FileNotFoundException error
和以下行:
{"errorCode":40100,"message":"Authorization required"}
我已按e.164
要求的格式输入电话号码,但无济于事。任何帮助都会很棒!
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());