Java - Perform oAuth1.0 authenticated request with given consumerKey, consumerSecret, accessToken, accessTokenSecret and realm

为君一笑 提交于 2020-07-22 10:06:46

问题


I am trying to send an http post to a given oAuth1.0 protected endpoint, the owner of the endpoint provided to me:

  • consumerKey
  • consumerSecret
  • accessToken
  • accessTokenSecret
  • realm

I wrote some code based on How to call API (Oauth 1.0)?

public class HttpAuthPost {
    public HttpAuthPost() {
        realmID = "XXXXXXX";
        String consumerKey = "kjahsdkjhaskdjhaskjdhkajshdkajsd";
        String consumerSecret = "jklahsdkjhaskjdhakjsd";
        String accessToken = "iuyhiuqhwednqkljnd";
        String accessTokenSecret = "oihkhnasdiguqwd56qwd";
        setupContext(consumerKey, consumerSecret, accessToken, accessTokenSecret);
    }

    public void setupContext(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret) {
        this.oAuthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
        oAuthConsumer.setTokenWithSecret(accessToken, accessTokenSecret);
        oAuthConsumer.setSigningStrategy(new AuthorizationHeaderSigningStrategy());

    }

    public void authorize(HttpRequestBase httpRequest) throws FMSException {
        try {
            oAuthConsumer.sign(httpRequest);
        } catch (OAuthMessageSignerException e) {
            throw new FMSException(e);
        } catch (OAuthExpectationFailedException e) {
            throw new FMSException(e);
        } catch (OAuthCommunicationException e) {
            throw new FMSException(e);
        }
    }

    public String executeGetRequest(String customURIString, String _content) throws UnsupportedEncodingException {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost httpRequest = null;
    //Preparing HttpEntity and populating httpRequest
        try {
            authorize(httpRequest);
        } catch (FMSException e) {
            e.printStackTrace();
        }
        HttpResponse httpResponse = null;
        try {
            HttpHost target = new HttpHost(uri.getHost(), -1, uri.getScheme());
            httpResponse = client.execute(target, httpRequest);
      // Process response and generate output
      return output;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

I did some tests and I am getting this error: USER_ERROR : header is not NLAuth scheme.

I noticed the realm value is never actually set in the oAuthConsumer configuration, I try to find a way to specify the realm but I have not found a way to do it.

Does anyone have a clue on this?


回答1:


Well the solution was actually pretty simple and now that I figured it out it seems obvious. Adding the realm as an additional parameter to the authconsumer worked for me.

Hope this help someone else in the future.

public void setupContext(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret) {
  this.oAuthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
  oAuthConsumer.setTokenWithSecret(accessToken, accessTokenSecret);
  oAuthConsumer.setSigningStrategy(new AuthorizationHeaderSigningStrategy());
  HttpParameters parameters = new HttpParameters();
  parameters.put("realm", realmID);
  oAuthConsumer.setAdditionalParameters(parameters);
}


来源:https://stackoverflow.com/questions/56102784/java-perform-oauth1-0-authenticated-request-with-given-consumerkey-consumerse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!