0
votes

I'm trying to obtain an Access Token and a Refresh Token by making a POST request to the https://login.eloqua.com/auth/oauth2/token endpoint.

I followed "Authenticate Using OAuth 2.0" document (Resource Owner Password Credentials grant flow).

I tried following code and I'm getting response code as 411 in response at the time of "conn.getInputStream()" (i.e issue with content length). I have already added the content length.

import java.io.BufferedReader;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.net.HttpURLConnection;  
import java.net.URL;  
import java.nio.charset.StandardCharsets; 
public class EloquaOath2App {  
  public static void main(String[] args) {  


    String siteName = "MarketingCloud08";  
    String username = "myAccountUsername";  
    String password = "myPassword";  
    String uri = "https://login.eloqua.com/auth/oauth2/token";  
    String client_id = "my-app-client-id";  
    String client_secret = "my-app-client-secret";  

    String authString = client_id + ":" + client_secret;// format is client_id:client_secret  
    String headerAuthorization = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(authString.getBytes());  
    String userAndSite = siteName + "\\" + username;  
      
    String response = null;  
    try {
        URL url = new URL(uri);  
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
        conn.setRequestMethod("POST");  

        conn.setRequestProperty("Content-Type", "application/json");  
        conn.setRequestProperty("Authorization", headerAuthorization);  
        conn.setRequestProperty("grant_type", "password");  
        conn.setRequestProperty("scope", "full");  
        conn.setRequestProperty("username", userAndSite); // The user’s sitename and username in the form sitename + '/' + username  
        conn.setRequestProperty("password", password);  

        byte[] postData = uri.getBytes(StandardCharsets.UTF_8);  
        int postDataLength = postData.length;  
        System.out.println("postDataLength      :" + postDataLength);  

        if (postDataLength > 0) {  
            // In case that the content is not empty  
            conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));  
        } else {  
            // In case that the content is not empty  
            conn.setRequestProperty("Content-Length", "0");  
        }  

        conn.setDoOutput(true);  

        InputStream is = conn.getInputStream();  

        BufferedReader rd = new BufferedReader(new InputStreamReader(is));  

        String line;  
        while ((line = rd.readLine()) != null) {  
            response += line;  
            System.out.println(response);  
        }  
        rd.close();  
        conn.disconnect();  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
  }
}

Exception log is :

java.io.IOException: Server returned HTTP response code: 411 for URL: https://login.eloqua.com/auth/oauth2/token
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
  at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
  at com.arpit.aem.eloquaconnector.core.models.EloquaOath2Test.main(EloquaOath2App.java:61)

Expected output will be like following:

{
  "access_token":"2YotnFZFEjr1zCsicMWpAA",
  "token_type":"bearer",
  "expires_in":3600,
  "refresh_token":"tGzv3JOkF0XG5Qx2TlKW"
}

Please guide me and suggest your suggestion/answer on this.

Thank you,

Arpit

1

1 Answers

0
votes

You have to write something to the connection, before reading.

conn.getOutputStream().write(postData);

And also pay attention that grant_type, scope etc. are have to be passed as a request body, and not as http headers. Use postman or something similar to make some test requests to rest api.