【问题标题】:Apache Camel Status Code 401 when send POST Request to PowerBI REST Endpoint向 PowerBI REST 端点发送 POST 请求时的 Apache Camel 状态代码 401
【发布时间】:2018-08-27 20:09:04
【问题描述】:

我正在尝试使用 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 进行此 POST 时,工作正常。

谢谢。

【问题讨论】:

    标签: apache-camel powerbi


    【解决方案1】:

    HTTP 401 表示“未经授权”。 您可能需要提供一些凭据。

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

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

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

    【讨论】:

    • 我联系了 PowerBI 团队,他们说这个端点没有用户/密码。使用 cURL、Postman、SOAPUI 等,我可以成功处理 POST 请求。你知道是否有办法在骆驼上做同样的事情?不要使用用户名/密码?谢谢!
    【解决方案2】:

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

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

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

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

    豆子:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多