views:

1241

answers:

4

I am working on integrating with the Photobucket API and I came across this in their api docs:

"Sort the parameters by name lexographically [sic] (byte ordering, the standard sorting, not natural or case insensitive). If the parameters have the same name, then sort by the value."

What does that mean? How do I sort something lexicographically? byte ordering?

The rest of their docs have been ok so far, but (to me) it seems like this line bears further explanation. Unfortunately there was none to be had.

Anyway, I'm writing the application in Python (it'll eventually become a Django app) in case you want to recommend specific modules that will handle such sorting for me ^_^

+4  A: 

The word should be "lexicographic"

http://www.thefreedictionary.com/Lexicographic

Dictionary order. Using the letters as they appear in the strings.

As they suggest, don't fold upper- and lower-case together. Just use the Python built-in list.sort() method.

S.Lott
+1  A: 

This is similar to the Facebook API — the query string needs to be normalized before generating the signature hash.

You probably have a dictionary of parameters like:

params = {
  'consumer_key': "....",
  'consumer_secret': "....",
  'timestamp': ...,
  ...
}

Create the query string like so:

urllib.urlencode(sorted(params.items()))

params.items() returns the keys and values of the dictionary as a list tuples, sorted() sorts the list, and urllib.urlencode() concatenates them into a single string while escaping.

a paid nerd
+4  A: 

I think that here lexicographic is a "alias" for ascii sort?

Lexicographic          Natural  
z1.doc                  z1.doc    
z10.doc                 z2.doc    
z100.doc                z3.doc    
z101.doc                z4.doc    
z102.doc                z5.doc    
z11.doc                 z6.doc    
z12.doc                 z7.doc    
z13.doc                 z8.doc    
z14.doc                 z9.doc     
z15.doc                z10.doc    
z16.doc                z11.doc    
z17.doc                z12.doc    
z18.doc                z13.doc     
z19.doc                z14.doc     
z2.doc                 z15.doc    
z20.doc                z16.doc    
z3.doc                 z17.doc    
z4.doc                 z18.doc    
z5.doc                 z19.doc    
z6.doc                 z20.doc    
z7.doc                z100.doc    
z8.doc                z101.doc    
z9.doc                z102.doc    
dfa
+1  A: 

Quote a bit more from the section:

2 Generate the Base String:

Normalize the parameters:

  • Add the OAuth specific parameters for this request to the input parameters, including:

    oauth_consumer_key = <consumer_key>
    oauth_timestamp = <timestamp>
    oauth_nonce = <nonce>
    oauth_version = <version>
    oauth_signature_method = <signature_method>
    
  • Sort the parameters by name lexographically [sic] (byte ordering, the standard sorting, not natural or case insensitive). If the parameters have the same name, then sort by the value.

  • Encode the parameter values as in RFC3986 Section 2 (i.e., urlencode). Create parameter string (). This is the same format as HTTP 'postdata' or 'querystring', that is, each parameter represented as name=value separated by &. For example, a=1&b=2&c=hello%20there&c=something%20else

I think that they are saying that the parameters must appear in the sorted order - oauth_consumer_key before oauth_nonce before ...

Jonathan Leffler