0

https://login.eloqua.com/auth/oauth2/tokenエンドポイントにPOSTリクエストを送信して、アクセス トークンリフレッシュ トークンを取得しようとしています。

「 Authenticate Using OAuth 2.0」ドキュメント(Resource Owner Password Credentials grant flow )に従いました。

次のコードを試してみましたが、「conn.getInputStream()」の時点で応答コード 411 を取得しています (つまり、コンテンツの長さの問題)。コンテンツの長さは既に追加しています。

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();  
    }  
  }
}

例外ログは次のとおりです。

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)

予想される出力は次のようになります。

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

私を導き、これに関する提案/回答を提案してください。

ありがとうございました、

アルピット

4

1 に答える 1