Ameritrade API POST请求JSON响应

时间:2018-12-23 23:04:15

标签: java json curl

我已经有好几年没有用JAVA编码了,我正在尝试将一种算法组合在一起,以便根据特定条件自动进行交易。

我希望使用Ameritrade API

我尝试在命令提示符下发送cURL消息,并且确实从服务器“无效密钥”获得了响应。我希望看到Java中出现“无效密钥”响应,因为这将证明我可以发送POST并将JSON对象接收回Java中。从那里开始,我将只进行一次身份验证!

以下是在命令提示符下发送的curl消息,该消息有效,请通过复制和粘贴自己尝试一下:

curl -X POST -H“内容类型:application / x-www-form-urlencoded” -d“ grant_type = authorization_code&refresh_token =&access_type = offline&code =&client_id =&redirect_uri =”“ https://api.tdameritrade.com/v1/oauth2/token

我想做的第一件事是能够在JAVA中发送此curl消息并在JAVA中接收回JSON响应

到目前为止,这是我要编写的代码,但是出现500错误,这使我将消息发送到服务器的方式与某些东西有关吗?

public void trytoAuthenticate() {
    HttpURLConnection connection = null;
    //
    //this is the curl message in command prompt you can send to receive JSON response back
    //curl -X POST --header "Content-Type: application/x-www-form-urlencoded" -d 
    //"grant_type=authorization_code&
    //refresh_token=&
    //access_type=offline&
    //code=&
    //client_id=&
    //redirect_uri=" "https://api.tdameritrade.com/v1/oauth2/token"

    try {
        //Create connection
        URL url = new URL("https://api.tdameritrade.com/v1/oauth2/token");
        String urlParameters = "grant_type=" + URLEncoder.encode("authorization_code", "UTF-8") + 
                "&refresh_token=" + URLEncoder.encode("", "UTF-8") + 
                "&access_type=" + URLEncoder.encode("", "UTF-8") + 
                "&code=" + URLEncoder.encode("", "UTF-8") + 
                "&client_id=" + URLEncoder.encode("", "UTF-8") + 
                "&redirect_uri=" + URLEncoder.encode("", "UTF-8");

        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST"); //-X
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //-H
        connection.setRequestProperty("Content-Length", 
                Integer.toString(urlParameters.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");  

        connection.setUseCaches(false);
        connection.setDoOutput(true);//connection will be output
        connection.setDoInput(true);//connection will be input

        //Send request
        DataOutputStream wr = new DataOutputStream (connection.getOutputStream());
        wr.writeBytes(urlParameters);
        System.out.println(urlParameters); //added for testing
        wr.close();

        //Get Response  
        DataInputStream is = new DataInputStream (connection.getInputStream());
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        rd.readLine();
        //StringBuffer response = new StringBuffer(); // or StringBuffer/StringBuilder if Java version 5+
        //String line;
        //while ((line = rd.readLine()) != null) {
        //  response.append(line);
        //  response.append('\r');
        //}
        rd.close();
        //System.out.println(response.toString());
        //return response.toString();
    } catch (Exception e) {
        e.printStackTrace();
        //return null;
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

几件事:

您需要四个参数:grant_type,access_type,redirect_url和代码。

  1. 您应该URL解码从您可能刚刚执行的浏览器登录名获得的授权代码(按照他们的指示)

  2. 删除空参数,仅保留我上面提到的内容。

  3. 重定向URL必须与在控制台中创建APP时添加的重定向URL完全匹配。

  4. 如果这是一个应用程序(看起来像它),则可能必须将access_type设置为“ offline”。再次查看其文档。取决于您的应用程序。

  5. grant_type应该是“ authorization_code”,因为这就是您想要的。

相关问题