0

hi i am trying to get request token in from google apis.for getting request token i have to sort the parameter string by lexicographical byte value ordering in java code segment for the string

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

i encoded parameters of query string now i have to lexicographically sort datastring for sending a post request to URL https://www.google.com/accounts/OAuthGetRequestToken can any one please help how to sort this string lexicographically in java.

mathlearner
  • 7,509
  • 31
  • 126
  • 189
  • What do you mean sort a string? Do you have many strings and wan't to sort them in order? – jlordo Jan 09 '13 at 09:42
  • no in this string there are many (key=value) pairs i have to sort these key value pairs in lexicographical order.so how to sort this string so that these key value pairs are in lexicographical order ?? – mathlearner Jan 09 '13 at 09:45
  • That should have been part of your question. Why don't just order your string concatenation following your desired order. All your keys are constant, so the order would be the same always. – jlordo Jan 09 '13 at 09:48
  • To make myself clear, why don't you start with `oauth_callback`, then `oauth_consumer_key` and so on. That will guarantee lexicographical order without having to sort afterwards. – jlordo Jan 09 '13 at 10:12
  • sorry actually i get confused with term lexicographical!! – mathlearner Jan 09 '13 at 12:07

1 Answers1

0

Use a Java TreeMap. This will allow you to use your OAuth parameters as key, then the value as the value you specified.

I suggest you add all your parameters first to the TreeMap, then iterate to get the Set<Entry<String, String>> to build your URL query string.

The Specification for TreeMap states:

The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228