使用Sinch无法发送短信(Android)

时间:2015-06-03 02:36:29

标签: java android sinch

我正在使用Sinch并按照教程,他们有通过Java发送短信的代码。但是,一旦我运行它,我得到一个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());

1 个答案:

答案 0 :(得分:0)

如果这是您正在使用的实时密钥,则需要对请求进行签名,我们仅允许对沙盒应用程序进行基本授权。了解更多信息pandas.read_csvhttps://www.sinch.com/tutorials/sign-requests-java/