Java中的双腿OAuth2示例?

Java中的双腿OAuth2示例?,java,android,oauth,Java,Android,Oauth,我有一个REST服务+OAuth2。在Python中,客户端应用程序经历了生成OAuth2使用者、请求、对其签名等过程。类似于以下内容(伪代码引用,不完整): 塔达!它就像一个冠军。我正试图在Java下做同样的事情,特别是在Android上,但在这一点上我什么都愿意做。我看到的所有示例都集中在使用三段式auth的公共OAuth2 API上,或者集中在web服务的服务器端配置上 有人能可怜我,给我举一个同样简单的Java示例,使用BasicHttpContext、DefaultHttpClient

我有一个REST服务+OAuth2。在Python中,客户端应用程序经历了生成OAuth2使用者、请求、对其签名等过程。类似于以下内容(伪代码引用,不完整):

塔达!它就像一个冠军。我正试图在Java下做同样的事情,特别是在Android上,但在这一点上我什么都愿意做。我看到的所有示例都集中在使用三段式auth的公共OAuth2 API上,或者集中在web服务的服务器端配置上

有人能可怜我,给我举一个同样简单的Java示例,使用BasicHttpContext、DefaultHttpClient和friends吗

以后编辑

回答自己的问题可能被认为是一种不好的形式,但下面是如何回答我的问题。事实证明,两条腿的oauth实际上是OAuthV1.0的功能,因此半废弃的oauth.signpost库可以轻松实现这一点。我试图使用最近维护的joauth库,但找不到任何好的示例来说明如何实现我想要的功能。在我的代码中,这段代码实际上分布在三到四个不同的java文件中,但我为这个伪代码示例一起完成了这段代码

import ...
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;

public class RestClient {

  private OAuthConsumer oauth_consumer = null;
  private DefaultHttpClient http_client;
  private static final String consumer_key    = "something";
  private static final String consumer_secret = "something_else";

  public RestClient( Context context ) {
    // Instantiate our custom http client with our trusted key
    this.http_client = new DefaultHttpClient();
    // Instantiate an oauth_consumer
    this.oauth_consumer = new CommonsHttpOAuthConsumer( consumer_key, consumer_secret );
  }

  public POST( String url, Map params ) {
    // Initialize our post request
    HttpPost httppost = new HttpPost( url );
    httppost.setHeader("Accept", "application/json");
    httppost.setHeader("Content-type", "application/x-www-form-urlencoded");

    // This iterates through the parameters and stores them for use
    List<NameValuePair> name_value_pairs = new ArrayList<NameValuePair>(2);
    Iterator iter = params.entrySet().iterator();
    while ( iter.hasNext() ) {
      Map.Entry pairs = (Map.Entry)iter.next();
      name_value_pairs.add(
        new BasicNameValuePair( (String)pairs.getKey(),   (String)pairs.getValue() )
      );
    }

    try {
      // Put our parameters in our Post
      httppost.setEntity( new UrlEncodedFormEntity( name_value_pairs ) );
      // sign our request
      this.oauth_consumer.sign( httppost );
      // Yoiks, and away!
      HttpResponse response = http_client.execute( httppost );
      HttpEntity entity = response.getEntity();

      if (entity != null) {
        InputStream instream = entity.getContent();
        JSONObject json = new JSONObject( convertStreamToString(instream) );
        // Closing the input stream will trigger connection release
        instream.close();
        return json;
      } catch ( aBunchOfShit e) {
        e.printStackTrace();
      }
    }
    return null;
  }
}
导入。。。
导入oauth.signpost.OAuthConsumer;
导入oauth.signpost.commonHTTP.commonHttpOAuthConsumer;
导入oauth.signpost.exception.OAuthCommunicationException;
导入oauth.signpost.exception.OAutheExpectationFailedException;
导入oauth.signpost.exception.oauthmessagesignereexception;
公共类RestClient{
私有OAuthConsumer oauth_consumer=null;
私有DefaultHttpClient http_客户端;
私有静态最终字符串使用者\u key=“something”;
私有静态最终字符串consumer\u secret=“something\u other”;
公共RestClient(上下文){
//使用可信密钥实例化自定义http客户端
this.http_client=新的DefaultHttpClient();
//实例化oauth_使用者
this.oauth_consumer=新的CommonHttpOAuth consumer(consumer_key,consumer_secret);
}
公共帖子(字符串url、映射参数){
//初始化我们的post请求
HttpPost HttpPost=新的HttpPost(url);
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
//这将遍历参数并存储它们以供使用
列表名称\值\对=新的ArrayList(2);
迭代器iter=params.entrySet().Iterator();
while(iter.hasNext()){
Map.Entry pairs=(Map.Entry)iter.next();
名称\u值\u对。添加(
新的BasicNameValuePair((字符串)pairs.getKey(),(字符串)pairs.getValue())
);
}
试一试{
//把我们的参数放在我们的帖子里
setEntity(新的UrlEncodedFormEntity(名称\值\对));
//签署我们的请求
此.oauth_消费者号(httppost);
//哎呀,走开!
HttpResponse response=http_client.execute(httppost);
HttpEntity=response.getEntity();
如果(实体!=null){
InputStream instream=entity.getContent();
JSONObject json=新的JSONObject(convertStreamToString(instream));
//关闭输入流将触发连接释放
流内关闭();
返回json;
}接球(紧靠e){
e、 printStackTrace();
}
}
返回null;
}
}

在后端验证签名时,请确保包含请求中传递的所有参数。我坐着看了一个小时的“无效签名。预期签名基字符串POST&…”错误,直到我知道如何设置System.setProperty(“调试”,“1”);看到了Java端的原始基字符串。现在这已经足够好了,但我仍然想看看它在维护的库下是如何工作的。

既然您提到了joauth,我已经编写了一个允许OAuth 1使用joauth进行授权的程序


我希望这能对您有所帮助。

既然您提到了joauth,我就写了一篇文章,允许OAuth 1使用joauth进行授权


我希望这能对您有所帮助。

我不明白您是如何使用
上下文作为
新建DefaultHttpClient
的参数的。DefaultHttpClient是否接受HttpParams?您使用的代码遵循OAuth 1.0a授权,但您的标题要求使用两条腿的OAuth。@LuxuryMode这只是一个输入错误。抱歉搞混了!我不明白您是如何使用
上下文
作为
新DefaultHttpClient
的参数的。DefaultHttpClient不接受HttpParams吗?您使用的代码遵循OAuth 1.0a授权,但您的标题要求使用两条腿的OAuth。@LuxuryMode这只是一个输入错误。抱歉搞混了!
import ...
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;

public class RestClient {

  private OAuthConsumer oauth_consumer = null;
  private DefaultHttpClient http_client;
  private static final String consumer_key    = "something";
  private static final String consumer_secret = "something_else";

  public RestClient( Context context ) {
    // Instantiate our custom http client with our trusted key
    this.http_client = new DefaultHttpClient();
    // Instantiate an oauth_consumer
    this.oauth_consumer = new CommonsHttpOAuthConsumer( consumer_key, consumer_secret );
  }

  public POST( String url, Map params ) {
    // Initialize our post request
    HttpPost httppost = new HttpPost( url );
    httppost.setHeader("Accept", "application/json");
    httppost.setHeader("Content-type", "application/x-www-form-urlencoded");

    // This iterates through the parameters and stores them for use
    List<NameValuePair> name_value_pairs = new ArrayList<NameValuePair>(2);
    Iterator iter = params.entrySet().iterator();
    while ( iter.hasNext() ) {
      Map.Entry pairs = (Map.Entry)iter.next();
      name_value_pairs.add(
        new BasicNameValuePair( (String)pairs.getKey(),   (String)pairs.getValue() )
      );
    }

    try {
      // Put our parameters in our Post
      httppost.setEntity( new UrlEncodedFormEntity( name_value_pairs ) );
      // sign our request
      this.oauth_consumer.sign( httppost );
      // Yoiks, and away!
      HttpResponse response = http_client.execute( httppost );
      HttpEntity entity = response.getEntity();

      if (entity != null) {
        InputStream instream = entity.getContent();
        JSONObject json = new JSONObject( convertStreamToString(instream) );
        // Closing the input stream will trigger connection release
        instream.close();
        return json;
      } catch ( aBunchOfShit e) {
        e.printStackTrace();
      }
    }
    return null;
  }
}