OAuth:如何通过java中的词典字节值排序对字符串进行排序

时间:2013-01-09 09:38:47

标签: java oauth 2-legged

嗨我正在尝试从google apis获取请求令牌。获取请求令牌我必须通过字典中的字典字节值排序对字符串

进行排序
String datastring=URLEncoder.encode("oauth_version", "UTF-8")+"="+URLEncoder.encode("1.0", "UTF-8");


  datastring+="&"+URLEncoder.encode("oauth_nonce", "UTF-8")+"="+URLEncoder.encode(System.currentTimeMillis()+"","UTF-8");

  datastring+="&"+URLEncoder.encode("oauth_timestamp", "UTF-8")+"="+URLEncoder.encode(System.currentTimeMillis()/1000+"","UTF-8");

    datastring+="&"+URLEncoder.encode("oauth_consumer_key","UTF-8")+"="+URLEncoder.encode("iriteshmehandiratta.appspot.com","UTF-8");

    datastring+="&"+URLEncoder.encode("oauth_signature_method","UTF-8")+"="+URLEncoder.encode("RSA-SHA1", "UTF-8"); 

    datastring+="&"+URLEncoder.encode("oauth_signature", "UTF-8")+"="+URLEncoder.encode(myrsakey, "UTF-8");

datastring+="&"+URLEncoder.encode("oauth_callback","UTF-8")+"="+URLEncoder.encode("https://www.iriteshmehandiratta.appspot.com/authsub","UTF-8");

datastring+="&"+URLEncoder.encode("scope","UTF-8")+"="+URLEncoder.encode("http://www.google.com/calendar/feeds","UTF-8");

我编码了查询字符串的参数现在我必须按字典顺序排序数据字符串,以便将发布请求发送到URL https://www.google.com/accounts/OAuthGetRequestToken 任何人都可以帮助如何在java中按字典顺序对这个字符串进行排序。

1 个答案:

答案 0 :(得分:0)

使用Java TreeMap。这样您就可以将OAuth参数用作键,然后将值用作您指定的值。

我建议您将所有参数第一个添加到TreeMap,然后重复以获取Set<Entry<String, String>>来构建您的网址查询字符串。

TreeMap州的规范:

  

地图根据其键的自然顺序排序,或者按照   在地图创建时提供Comparator,具体取决于哪个   使用构造函数。