-4

我正在使用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());
4

1 回答 1

0

如果这是您使用的实时密钥,您需要签署请求,我们只允许对沙盒应用程序进行基本授权。有关更多信息https://www.sinch.com/tutorials/sign-requests-java/https://www.sinch.com/docs/sms/#Authorization

于 2015-06-09T20:50:54.920 回答