title: Python urllib2, basic HTTP authentication, and tr.im
body: <p>I'm playing around, trying to write some code to use the <a href="http://www.programmableweb.com/api/tr.im" rel="noreferrer">tr.im</a>
  APIs to shorten a URL.</p>
  
  <p>After reading <a href="http://docs.python.org/library/urllib2.html" rel="noreferrer">http://docs.python.org/library/urllib2.html</a>, I tried:</p>
  
  <pre><code>   TRIM_API_URL = 'http://api.tr.im/api'
     auth_handler = urllib2.HTTPBasicAuthHandler()
     auth_handler.add_password(realm='tr.im',
                               uri=TRIM_API_URL,
                               user=USERNAME,
                               passwd=PASSWORD)
     opener = urllib2.build_opener(auth_handler)
     urllib2.install_opener(opener)
     response = urllib2.urlopen('%s/trim_simple?url=%s'
                                % (TRIM_API_URL, url_to_trim))
     url = response.read().strip()
  </code></pre>
  
  <p>response.code is 200 (I think it should be 202).  url is valid, but
  the basic HTTP authentication doesn't seem to have worked, because the
  shortened URL isn't in my list of URLs (at <a href="http://ttp://tr.im/?page=1" rel="noreferrer">http://tr.im/?page=1</a>).</p>
  
  <p>After reading <a href="http://www.voidspace.org.uk/python/articles/authentication.shtml#doing-it-properly" rel="noreferrer">http://www.voidspace.org.uk/python/articles/authentication.shtml#doing-it-properly</a>
  I also tried:</p>
  
  <pre><code>   TRIM_API_URL = 'api.tr.im/api'
     password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
     password_mgr.add_password(None, TRIM_API_URL, USERNAME, PASSWORD)
     auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
     opener = urllib2.build_opener(auth_handler)
     urllib2.install_opener(opener)
     response = urllib2.urlopen('http://%s/trim_simple?url=%s'
                                % (TRIM_API_URL, url_to_trim))
     url = response.read().strip()
  </code></pre>
  
  <p>But I get the same results.  (response.code is 200 and url is valid,
  but not recorded in my account at <a href="http://tr.im/" rel="noreferrer">http://tr.im/</a>.)</p>
  
  <p>If I use query string parameters instead of basic HTTP authentication,
  like this:</p>
  
  <pre><code>   TRIM_API_URL = 'http://api.tr.im/api'
     response = urllib2.urlopen('%s/trim_simple?url=%s&amp;username=%s&amp;password=%s'
                                % (TRIM_API_URL,
                                   url_to_trim,
                                   USERNAME,
                                   PASSWORD))
     url = response.read().strip()
  </code></pre>
  
  <p>...then not only is url valid but it's recorded in my tr.im account.
  (Though response.code is still 200.)</p>
  
  <p>There must be something wrong with my code though (and not tr.im's API), because</p>
  
  <pre><code>$ curl -u yacitus:xxxx http://api.tr.im/api/trim_url.json?url=http://www.google.co.uk
  </code></pre>
  
  <p>...returns:</p>
  
  <pre><code>{"trimpath":"hfhb","reference":"nH45bftZDWOX0QpVojeDbOvPDnaRaJ","trimmed":"11\/03\/2009","destination":"http:\/\/www.google.co.uk\/","trim_path":"hfhb","domain":"google.co.uk","url":"http:\/\/tr.im\/hfhb","visits":0,"status":{"result":"OK","code":"200","message":"tr.im URL Added."},"date_time":"2009-03-11T10:15:35-04:00"}
  </code></pre>
  
  <p>...and the URL does appear in my list of URLs on <a href="http://ttp://tr.im/?page=1" rel="noreferrer">http://tr.im/?page=1</a>.</p>
  
  <p>And if I run:</p>
  
  <pre><code>$ curl -u yacitus:xxxx http://api.tr.im/api/trim_url.json?url=http://www.google.co.uk
  </code></pre>
  
  <p>...again, I get:</p>
  
  <pre><code>{"trimpath":"hfhb","reference":"nH45bftZDWOX0QpVojeDbOvPDnaRaJ","trimmed":"11\/03\/2009","destination":"http:\/\/www.google.co.uk\/","trim_path":"hfhb","domain":"google.co.uk","url":"http:\/\/tr.im\/hfhb","visits":0,"status":{"result":"OK","code":"201","message":"tr.im URL Already Created [yacitus]."},"date_time":"2009-03-11T10:15:35-04:00"}
  </code></pre>
  
  <p>Note code is 201, and message is "tr.im URL Already Created [yacitus]."</p>
  
  <p>I must not be doing the basic HTTP authentication correctly (in either attempt).  Can you spot my problem?  Perhaps I should look and see what's being sent over the wire?  I've never done that before.  Are there Python APIs I can use (perhaps in pdb)?  Or is there another tool (preferably for Mac OS X) I can use?</p>