【问题标题】:Java - Perform oAuth1.0 authenticated request with given consumerKey, consumerSecret, accessToken, accessTokenSecret and realmJava - 使用给定的 consumerKey、consumerSecret、accessToken、accessTokenSecret 和 realm 执行 oAuth1.0 认证请求
【发布时间】:2019-05-15 00:54:42
【问题描述】:

我正在尝试向给定的受 oAuth1.0 保护的端点(提供给我的端点的所有者)发送 http 帖子:

  • consumerKey
  • 消费者秘密
  • 访问令牌
  • accessTokenSecret
  • 领域

我根据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;
    }
}

我做了一些测试,我收到了这个错误:USER_ERROR : header is not NLAuth scheme。

我注意到域值实际上从未在 oAuthConsumer 配置中设置,我试图找到一种方法来指定域,但我还没有找到方法。

有人知道吗?

【问题讨论】:

    标签: java oauth httprequest


    【解决方案1】:

    嗯,解决方案实际上非常简单,现在我想通了,这似乎很明显。将领域作为附加参数添加到 authconsumer 对我有用。

    希望这对将来的其他人有所帮助。

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

    【讨论】:

    • 我正在使用相同的代码方式,唯一的变化是我将传递 URL 而不是 realmID。你能告诉我怎么做吗?抱歉,我是 API 新手。
    猜你喜欢
    • 2018-03-13
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 2021-09-26
    • 2017-12-17
    • 2016-06-02
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多