【问题标题】:Getting 401 when signing HTTP message with signpost使用路标签署 HTTP 消息时出现 401
【发布时间】:2016-09-24 06:08:06
【问题描述】:

我正在将我的网络应用程序与AppDirect 集成, 为此,我使用 jersey 创建了一个 java rs API。

当我订阅一个事件时,我会得到一个包含 oauth 值(密钥和秘密)的映射,用于对我的请求进行签名,以及一个事件 url,我向其发出签名获取。

我按预期获得了这些值(oauth 和 eventurl)。

现在,当我尝试使用库 signpost 发出签名提取时,我使用以下代码:

OAuthConsumer consumer = new DefaultOAuthConsumer(consumer_key, secret);
// create an HTTP request to a protected resource
URL url = new URL(eventUrl);
HttpURLConnection request = (HttpURLConnection) url.openConnection();

// sign the request
consumer.sign(request);

// send the request
request.connect();

我收到此错误消息:

getResponseMessage: Unauthorized
getresponsecode: 401

我还尝试了以下测试值:

  1. url = "https://www.appdirect.com/api/integration/v1/events/dummyOrder";
  2. dummyKey = "假人";
  3. dummySecret = "秘密"; 但我得到了同样的结果。

请问我该如何解决?

我也试过添加这个:

request.setRequestMethod("GET");         

request.setRequestProperty("Authorization", "OAuth");

request.setRequestProperty("Host", "...");

request.setRequestProperty("Content-Type", "application/xml");         

request.setRequestProperty("oauth_nonce", oauth_nonce);

request.setRequestProperty("oauth_signature", oauth_signature);

request.setRequestProperty("oauth_signature_method", oauth_signature_method);

request.setRequestProperty("oauth_timestamp", oauth_timestamp);

request.setRequestProperty("oauth_version", oauth_version);

还尝试在 Authorization 属性中使用 key:secret

【问题讨论】:

    标签: java oauth httpurlconnection signpost appdirect


    【解决方案1】:

    这是通过 Postman Chrome 扩展程序进行测试时此服务的行为。 如果您使用的是 OAuth 提供程序,那么您需要获取 AppDirect 的有效 api-key 和 secret。

    顺便说一句,第二个屏幕截图显示您不需要将 OAuth 令牌发送到 appdirect 到 https://www.appdirect.com/api/integration/v1/events/dummyOrder,因为它授权任何 url。

    因此,根据您的注释,您必须添加正确的(秘密和密钥),然后 AppDirect OAuth 服务器将返回一个有效令牌,您将在寻址 AppDirect 的存储库时使用该令牌。或者您可以在每个请求中发送密钥。

    【讨论】:

    • 是的,我已经使用 OAuthConsumer consumer = new DefaultOAuthConsumer(consumer_key, secret);我将添加一些代码
    • 我确实有一个有效的消费者密钥和秘密,我将它传递给我的 DefaultOAuthConsumer 对象。我还能在哪里使用它?
    • 按照我试过的方法试试。使用邮递员或其他休息客户端。添加您从 java-code 使用的凭据。在此处发布结果。
    • 实际上,它适用于测试密钥,但不适用于他们提供的那些不起作用,无论如何谢谢
    猜你喜欢
    • 2015-07-22
    • 2011-02-23
    • 2018-11-07
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多