【问题标题】:Oauth token requests before provider credentials issuance提供者凭证颁发之前的 Oauth 令牌请求
【发布时间】:2016-04-26 15:30:00
【问题描述】:

如果我问了一些愚蠢的问题,请原谅我,我是新手。我需要在我的 Java 应用程序中实现 OAuth 以针对 launchpad.net API 进行身份验证。 documentation 指定使用三个参数启动令牌请求:oauth_consumer_key 例如(我的应用程序的名称),oauth_signature_method 例如“PLAINTEXT”和 oauth_signature 例如字符串“&”。我意识到大多数 OAuth 库都需要 我已经从 OAuth 提供者(例如在 Twitter 中发布的),并且大多数示例都是以这种方式组织的。但是,launchpad.net 只有在发出请求令牌后才会发出这些参数(它们不使用第三方提供商)。我该如何继续?在尝试了一些引发错误的库后,我目前陷入困境。非常感谢您提供任何有用的信息。官方的启动板库是在 python 中的。

我的初始代码如下:

public class Quicky {

    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpGet = new HttpGet("https://launchpad.net/+request-token");
            CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
                System.out.println("Your current GET request status:" + response1.getStatusLine());
                HttpEntity entity1 = response1.getEntity();
  EntityUtils.consume(entity1);
            } finally {
                response1.close();
            }

            HttpRequest request;
            HttpPost httpPost = new HttpPost("https://launchpad.net/+request-token");
            PostMethod poster = new PostMethod();
            List <NameValuePair> postParams = new ArrayList <NameValuePair>();
            postParams.add(new BasicNameValuePair("oauth_customer_key", "XXXX"));
            postParams.add(new BasicNameValuePair("oauth_signature_method", "PLAINTEXT"));
            postParams.add(new BasicNameValuePair("oauth_signature", "&"));
   httpPost.setEntity(new UrlEncodedFormEntity(postParams, "utf-8"));
//            httpPost.setEntity(entity1);
            httpclient.execute(httpPost);

            HttpParameters requestParams = (HttpParameters) postParams;
            CloseableHttpResponse response2 = httpclient.execute(httpPost);
            try {
                System.out.println("Your current POST request status:" + response2.getStatusLine());
                HttpEntity entity2 = response2.getEntity();
                // do something useful with the response body
                // and ensure it is fully consumed
                EntityUtils.consume(entity2);
            } finally {
                response2.close();
            }
        } finally {
            httpclient.close();
        }
    }

}

【问题讨论】:

  • 展示您迄今为止尝试过的内容并具体说明。您有一个很好的问题,但很可能因为您没有提供足够的信息而被关闭。
  • @MadPhysicist 感谢您的有用观察。我已经添加了我的代码。我跑步时收到 401。

标签: java rest oauth launchpad signpost


【解决方案1】:

经过一些研究和代码重构,我终于解决了问题错误消息。正确的代码如下,也许它对那里的人有用。

    public class LaunchPadTokenRetriever {  

   public static void main(String[] args) throws ClientProtocolException,       IOException{

    CloseableHttpClient httpclient = HttpClients.createDefault();

    HttpPost httpPost = new HttpPost("https://launchpad.net/+request-token");
    httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");

    List <NameValuePair> urlParams = new ArrayList <NameValuePair>();
    urlParams.add(new BasicNameValuePair("oauth_signature", "&"));
    urlParams.add(new BasicNameValuePair("oauth_consumer_key", "tester"));
    urlParams.add(new BasicNameValuePair("oauth_signature_method", "PLAINTEXT"));
    httpPost.setEntity(new UrlEncodedFormEntity(urlParams));
    CloseableHttpResponse response = httpclient.execute(httpPost);
    System.out.println(response);

    try {
      System.out.println(response.getStatusLine());
      HttpEntity entity = response.getEntity();
      ResponseHandler<String> responseHandler = new BasicResponseHandler();
      String responseBody =  httpclient.execute(httpPost, responseHandler);
      System.out.println("Initial credentials ---> "+ responseBody);
      System.out.println();    
      String getresponse = responseBody;

      EntityUtils.consume(entity);
    } finally {
      response.close();
    }
  }

}

【讨论】:

    猜你喜欢
    • 2014-11-15
    • 2021-05-28
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多