403 error - JAVA 1.8

Our system does not support java.net.http.HttpClient, so I have to use java.net.URLConnection

 

I got 403 error

403 Forbidden
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://authz.constantcontact.com/oauth2/default/v1/token?code={authcode}&redirect_uri={url}&grant_type=authorization_code

 

Here is my Java code,

        URL url = new URL("https://authz.constantcontact.com/oauth2/default/v1/token?code="+authCode+"&redirect_uri="+redirectUri+"&grant_type=authorization_code");
        HttpURLConnection http = (HttpURLConnection)url.openConnection();
        http.setRequestMethod("POST");
        http.setDoOutput(true);
        http.setRequestProperty("Accept", "application/json");
        http.setRequestProperty("Authorization", authHeader);
        http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        http.setRequestProperty("Content-Length", "0");
       
        System.out.println(http.getResponseCode() + " " + http.getResponseMessage());

       
        StringBuffer temp = new StringBuffer();
        Scanner scan = new Scanner(http.getInputStream(), "UTF-8");
        // Scanner scan = new Scanner(http.getInputStream());
        while (scan.hasNext()) {
           temp.append(scan.nextLine());
        }
        scan.close();
           
        http.disconnect();
1 reply

Leaderboard