0

I am trying to generate token with Orange SMS API with Java.

So far I've got the following errors file not found exceptions, or error 411 when I take postdata.

Here is my code:

String  url = "https://api.orange.com/oauth/token";
 try {
    URL obj = new URL(url);
    String  postdata = "grant_type=client_credentials";

     HttpURLConnection con = (HttpURLConnection) obj.openConnection();
     con.setDoOutput(true);
     con.setDoInput(true);
     con.setRequestMethod("POST");
     con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

     String credentials = "client-id" + ":" + "client-secret";
     String base64EncodedCreds = Base64.getEncoder().encodeToString(credentials.getBytes());
      con.setRequestProperty ("Authorization", "Basic " + base64EncodedCreds);

     OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
     wr.write(postdata);
     wr.flush();
     wr.close();

     ByteArrayOutputStream rspBuff = new ByteArrayOutputStream();
     InputStream rspStream = con.getInputStream();
     int c;
     while ((c = rspStream.read())>0){
         rspBuff.write(c);

     }

     } catch (Exception e) {
         e.printStackTrace();
     }
 }
4

1 回答 1

0

您在代码中用于请求令牌的 URL 错误,您忘记了v2

String  url = "https://api.orange.com/oauth/v2/token";

(在适度之前尝试理解答案可能会很棒)

于 2017-08-23T08:28:01.567 回答