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