发送POST请求到PowerBI REST端点时的Apache Camel状态代码401

时间:2018-08-22 18:12:19

标签: apache-camel powerbi

我正在尝试使用Apache Camel消耗来自JMS队列的消息,并通过终端休息将其传递到PowerBI。

代码下方:

public class FromQueueToPowerBI extends RouteBuilder {

@Override
public void configure() throws Exception {

    InitialContext context = new InitialContext();

    Queue queue = (Queue) context.lookup("java:jboss/exported/FROM.QUEUE");

    from("amq:" + queue.getQueueName())
    .routeId("fromQueueToPowerBI")
    .autoStartup(true)
    .removeHeaders("*")
    .setHeader("CamelHttpMethod", constant("POST"))
    .setHeader("Content-Type", constant("application/json"))
    .log(">>>>>> MESSAGE: ${body}")
    .to("https4:api.powerbi.com/beta/e0...eb/datasets/72...fb/rows?key=fTD...%3D");

}

}

我收到此错误:

org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking https://api.powerbi.com/beta/e07...feb/datasets/72...5fb/rows?key=fT...3D with statusCode: 401

当我尝试通过cURL或Postman进行此发布时,工作正常。

谢谢。

2 个答案:

答案 0 :(得分:1)

HTTP 401的意思是“未授权”。 您可能必须提供一些凭据。

请参见http://camel.apache.org/http.html中的“ authMethod”,“ authUsername”和“ authPassword”参数 其他可能性:直接填充“授权”标头:

.setHeader("Authorization", constant("Basic xxxxxxxx"))
.to("https4:...)

其中xxxxxxxx是用Base64编码的“用户名:密码”

答案 1 :(得分:0)

尝试了许多方法来解决该问题,但不幸的是没有成功。

所以,我做了一个解决方法。

我用doPost(String message)方法创建一个Bean。 而是使用.to(),我调用此方法,并且帖子工作正常。

.bean(BeanPost.class, "doPost("${body})")

Bean:

public class BeanPost {

static HttpURLConnection con;

public String doPost(String body) throws IOException {

    CloseableHttpClient client = HttpClientBuilder.create().build();

    HttpPost httpPost = new HttpPost("destionation");
    httpPost.setHeader("Content-type", "application/json");
    httpPost.setHeader("User-Agent", "Java client");

    try {
        StringEntity stringEntity = new StringEntity(body);
        httpPost.getRequestLine();
        httpPost.setEntity(stringEntity);

        client.execute(httpPost);

        return "OK";
    } catch (Exception e) {
        return e.getMessage();
    }
}