I play trying to write code to use the tr.im API to shorten the url.
After reading http://docs.python.org/library/urllib2.html, I tried:
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()
response.code is 200 (I think it should be 202). The url is valid, but standard HTTP authentication did not seem to work, since the Abbreviated URL is not listed in my list of URLs ( http://tr.im/?page=1 ).
After reading http://www.voidspace.org.uk/python/articles/authentication.shtml#doing-it-properly I also tried:
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()
But I get the same results. (response.code is 200, and the url is valid, but not recorded in my account at http://tr.im/ .)
If I use the query string parameters instead of basic HTTP authentication, for example:
TRIM_API_URL = 'http://api.tr.im/api' response = urllib2.urlopen('%s/trim_simple?url=%s&username=%s&password=%s' % (TRIM_API_URL, url_to_trim, USERNAME, PASSWORD)) url = response.read().strip()
... then not only the URL is valid, but it will also be written to my tr.im account. (Although response.code is still 200.)
There should be something wrong in my code (not the tr.im API), because
$ curl -u yacitus:xxxx http://api.tr.im/api/trim_url.json?url=http://www.google.co.uk
... returns:
{"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"}
... and the URL appears in my list of URLs at http://tr.im/?page=1 .
And if I run:
$ curl -u yacitus:xxxx http://api.tr.im/api/trim_url.json?url=http://www.google.co.uk
... again, I get:
{"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"}
The note code is 201 and the message is "The URL tr.im has already been created [yacitus]."
I do not have to perform basic HTTP authentication correctly (in any attempt). Can you identify my problem? Perhaps I should look and see what is sent by wire? I have never done this before. Are there any Python APIs that I can use (possibly in pdb)? Or is there another tool (preferably for Mac OS X) that I can use?