将curl http请求转换为Java的错误请求

时间:2017-02-07 19:46:01

标签: java rest http curl resttemplate

我有curl的以下请求,可以毫无问题地与Microsoft Azure服务进行对话。

curl --request POST https://login.microsoftonline.com/common/oauth2/v2.0/token --data 'client_id=fe37...06-566f5c762ab2&grant_type=authorization_code&client_secret=tPv..dQfqomaG&scope=mail.read&code=OAQABAAIA...gAA'

以下是抛出Bad Request异常的java代码:

 public String getToken(String authCode){

        try {

            HttpHeaders headers = new HttpHeaders();

            String url = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
            UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
            headers.add("client_id", "fe3..b2");
            headers.add("client_secret", "tP..aG");
            headers.add("grant_type", "authorization_code");
            headers.add("code", authCode);
            headers.add("scope", "mail.read");


            HttpEntity<?> entity = new HttpEntity<>(headers);
            RestTemplate restTemplate = new RestTemplate();

            HttpEntity<String> response = restTemplate.exchange(builder.build().toUri(), HttpMethod.POST, entity, String.class);


        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }

我也尝试将--data部分添加到参数对象中,我收到了同样的问题。我正在使用RestTemplate,但我愿意接受其他建议。

我知道你的帮助。

2 个答案:

答案 0 :(得分:1)

我认为问题在于def freightResults = result.list { projections { groupProperty "polCode" // 0 groupProperty "podCode" // 1 groupProperty "ctnSize" // 2 groupProperty "carNam" // 3 groupProperty "curCode" // 4 groupProperty "motCode" // 5 count() min('unitValue') max('unitValue') med('unitValue') // HOW CREATE THIS CUSTOM FUNCTION } } 示例中您将这些参数传递到POST主体内,而在您的java代码中则使用标头代替。尝试将其更改为使用curl对象的body参数:

entity

答案 1 :(得分:0)

您需要在格式为url encoded格式的请求实体中发送这些参数,并将内容类型设置为// ... output reg cout; output reg [BIT_WIDTH-1:0] s; wire [BIT_WIDTH-1:0] s_wire; wire [BIT_WIDTH:0] cin_wire; assign cin_wire[0] = cin; genvar i; generate for(i = 0; i <= BIT_WIDTH-1; i = i + 1) begin fadder fadder_inst (.s(s_wire[i]), .cout(cin_wire[i+1]), .a(a[i]), .b(b[i]), .cin(cin_wire[i])); end endgenerate always@(posedge clk) begin s <= s_wire; cout <= cin_wire[BIT_WIDTH]; end // ...

你的身体可以是一个字符串(根据你的例子):

application/x-www-form-urlencoded

设置内容类型标题:

String data = "client_id=fe37...06-566f5c762ab2&grant_type=authorization_code&client_secret=tPv..dQfqomaG&scope=mail.read&code=OAQABAAIA...gAA";
HttpEntity<String> entity = new HttpEntity<>(data);

(实际实施取决于您使用的库)