使用OAuth-Signpost和Apache HttpComponents签署POST请求的正确方法是什么?

ran*_*au5 12 java oauth http-post signpost apache-httpcomponents

我目前正在使用OAuth-Signpost Java库来签署从客户端发送到实现OAuth身份验证的服务器的请求.在进行GET请求时(使用HttpURLConnection)一切正常:请求被签名,参数被包含,签名在目的地中匹配.但是,它似乎不适用于POST请求.我知道使用HttpURLConnection签名POST时可能出现的问题,因此我转移到Apache HttpComponents库以获取这些请求.我在以下示例中发送的参数是纯字符串和类似XML的字符串('rxml').我的代码如下:

public Response exampleMethod(String user, String sp, String ep, String rn, String rxml){

   //All these variables are proved to be correct (they work right in GET requests)
    String uri = "...";
    String consumerKey = "...";
    String consumerSecret = "...";
    String token = "...";
    String secret = "...";

  //create the parameters list
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("user", user));
    params.add(new BasicNameValuePair("sp", sp));
    params.add(new BasicNameValuePair("ep", ep));
    params.add(new BasicNameValuePair("rn", rn));
    params.add(new BasicNameValuePair("rxml", rxml));

   // create a consumer object and configure it with the access
   // token and token secret obtained from the service provider
    OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
    consumer.setTokenWithSecret(token, secret);

   // create an HTTP request to a protected resource
    HttpPost request = new HttpPost(uri);

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

   // set the parameters into the request
    request.setEntity(new UrlEncodedFormEntity(params));

   // send the request
    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(request);

   //if request was unsuccessful
    if(response.getStatusLine().getStatusCode()!=200){
        return Response.status(response.getStatusLine().getStatusCode()).build();
    }

   //if successful, return the response body
    HttpEntity resEntity = response.getEntity();
    String responseBody = "";

    if (resEntity != null) {
        responseBody = EntityUtils.toString(resEntity);
    }

    EntityUtils.consume(resEntity);

    httpClient.getConnectionManager().shutdown();

    return Response.status(200).entity(responseBody).build();

}
Run Code Online (Sandbox Code Playgroud)

当我向服务器发送POST请求时,我收到一个错误,告知签名(我发送的签名和服务器自己计算的签名)不匹配,所以我想这与他们签署的基本字符串有关以及POST签名的工作方式,因为他们在双方处理相同的密钥和秘密(已选中).

我已经读过,这样做的一种方法是将参数设置为URL的一部分(如在GET请求中).但它对我不起作用,因为XML参数可能超过URL长度,因此需要将其作为POST参数发送.

我想我在签署POST请求或处理参数时做错了什么,但我不知道它是什么.拜托,你能救我​​吗?

PS:如果我缺乏关于这个问题的背景,错误痕迹或其他信息,我很抱歉,但我是新手.如果您需要,请不要犹豫,向我询问更多信息.

Hra*_*afn 5

一点背景/解释

在过去的几天里我一直有类似的问题,几乎放弃了.直到我听说我公司的那个人正在提供我正在与之通信的服务,他们已经将它们配置为从查询字符串而不是标头参数中读取OAuth信息.

因此,例如[Authorization: OAuth oauth_consumer_key="USER", oauth_nonce="4027096421883800497", oauth_signature="Vd%2BJEb0KnUhEv1E1g3nf4Vl3SSM%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1363100774", oauth_version="1.0"],当您将其传递给要签名时,例如,尝试读取查询字符串的服务,而不是从标头参数Authorization中读取它,而Signpost会将其放入请求中http://myservice.mycompany.com?oauth_consumer_key=USER&oauth_nonce=4027096421883800497&oauth_signature=Vd%2BJEb0KnUhEv1E1g3nf4Vl3SSM%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1363100774&oauth_version=1.0.

这个问题是当我尝试签署url然后用它构建一个HttpPost请求时,url得到了一个带有前缀GET而不是POST的基本字符串,这给了另一个签名,然后是服务计算出来的签名.路标没有做错任何事情,它的网址签名方法默认设置为GET,没有其他可能的开箱即用.之所以如此,是因为你应该在POST时读取头参数,而不是查询字符串(Going to egg the house of the"my colle"),Signpost会在签署请求时添加这些,这在POST时应该做.

signedbasestring可以在Signpost中生成的SigningBaseString类方法中观察到.

现在我就是这样做的,但其他方式可能甚至更好.

  1. 获取路标源代码并将其添加到项目中.可以在这里得到它
  2. 找到OAuthConsumer该类并更改签名方法,以便您可以传递请求应为POST的信息.在我的情况下,我添加了一个像这样的布尔值public String sign(String url, boolean POST)
  3. 现在您需要更改类中的sign方法AbstractOAuthConsumer,CommonsHttpOAuthConsumer并进行DefaultOAuthConsumer扩展.在我的例子中,我if(POST) request.setMethod("POST");在方法调用之前将boolean变量添加到方法和右下方sign(request);
  4. 现在请求是一个Signpost特定对象,HTTPRequest,所以这将引发错误.您需要更改它并添加方法public void setMethod(String method);.
  5. 这将导致以下类中的错误HttpURLConnectionRequestAdapter,HttpRequestAdapter并且UrlStringRequestAdapter.您需要将方法实现添加到所有方法,但方式不同.首先你要添加

    public void setMethod(String method){
    try {
     this.connection.setRequestMethod(method);
     } catch (ProtocolException e) {
    
        e.printStackTrace();
     }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    对于你要添加的第二个

    public void setMethod(String method){
    
       try {
           RequestWrapper wrapper = new RequestWrapper(this.request);
           wrapper.setMethod(method);
           request = wrapper;
       } catch (org.apache.http.ProtocolException e) {
        e.printStackTrace();
        }   
    }
    
    Run Code Online (Sandbox Code Playgroud)

    并且最后你会添加

    public void setMethod(String method){
       mMethod = method;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    请注意,我只使用并尝试了第一个和最后一个.但这至少可以让你知道如何解决问题,如果你和我一样.

无论如何,希望这会有所帮助.

-MrDresden