Python 3 Basic Authentication

1,843 views
Skip to first unread message

Gerald Bäck

unread,
May 31, 2009, 11:11:51 AM5/31/09
to twitter-deve...@googlegroups.com

Hi,

I try to do my first steps with the Twitter API, but I always get a 401 with this python 3 code. I douplechecked the credentials twice, they are surely correct. What am I doing wrong. Thankks in advance,

Gerald

auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm="Twitter API",
uri="http://example.com",
user="123",
passwd="123")
opener = urllib.request.build_opener(auth_handler)

urllib.request.install_opener(opener)
f = urllib.request.urlopen('http://twitter.com/friendships/destroy/scoop_at.json')
print(f.read())

Jason Emerick

unread,
May 31, 2009, 12:05:46 PM5/31/09
to twitter-deve...@googlegroups.com
I would recommend just added the authorization header directly to the request versus using the basic auth handler.  I have included some sample code below of how I have been doing it.

username = 'twitter'
password = 'twitter'
basic = base64.encodestring('%s:%s' % (username, password))[:-1]

request = urllib2.Request('http://stream.twitter.com/spritzer.json')

request.add_header('Authorization', 'Basic %s' % basic)

spritzer = urllib2.urlopen(request)

Jason Emerick

The information transmitted (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is intended only for the person(s) or entity/entities to which it is addressed and may contain confidential and/or privileged material.  Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient(s) is prohibited.  If you received this in error, please contact the sender and delete the material from any computer.

Paul Kinlan

unread,
May 31, 2009, 12:25:36 PM5/31/09
to twitter-deve...@googlegroups.com
I know it is not exactly the same service, but when I authenticate against the Twitter Stream API using basic auth and python I set the Realm = None when I call add_password on the basic auth handler.

Paul

2009/5/31 Jason Emerick <jeme...@gmail.com>
Reply all
Reply to author
Forward
0 new messages